Introduction to PHP Chapter 9 Arrays Array Definition

![Array Definition Arrays are created with the array() constructor: $Array. Name = array([(mixed)…]) n Array Definition Arrays are created with the array() constructor: $Array. Name = array([(mixed)…]) n](https://slidetodoc.com/presentation_image_h/22868fdb039756d2cbe52c2dea69807e/image-2.jpg)










![Quadratic formula (cont. ) Document 9. 9 b (quadrat_2. php) <? php var_dump($_POST["coeff"]); echo Quadratic formula (cont. ) Document 9. 9 b (quadrat_2. php) <? php var_dump($_POST["coeff"]); echo](https://slidetodoc.com/presentation_image_h/22868fdb039756d2cbe52c2dea69807e/image-13.jpg)

![Document 9. 10 b (Cloud. Obs. php) <? php $high = $_POST["high"]; PHP builds Document 9. 10 b (Cloud. Obs. php) <? php $high = $_POST["high"]; PHP builds](https://slidetodoc.com/presentation_image_h/22868fdb039756d2cbe52c2dea69807e/image-15.jpg)


- Slides: 17

Introduction to PHP – Chapter 9 Arrays
![Array Definition Arrays are created with the array constructor Array Name arraymixed n Array Definition Arrays are created with the array() constructor: $Array. Name = array([(mixed)…]) n](https://slidetodoc.com/presentation_image_h/22868fdb039756d2cbe52c2dea69807e/image-2.jpg)
Array Definition Arrays are created with the array() constructor: $Array. Name = array([(mixed)…]) n By default, PHP arrays have user-defined index (key) values: $a = array($key 1 => $value 1, $key 2 => $value 2, $key 3 => $value 3, …); Keys can be numbers, characters, or strings. Numerical keys can start with any value, not just 0. n

Document 9. 1. (keyed. Array. php) <? php // Create an array with user-specified keys. . . echo ' A keyed array: '; $stuff = array('mine' => 'BMW', 'yours' => 'Lexus', 'ours' => 'house'); foreach ($stuff as $key => $val) { echo '$stuff['. $key. '] = '. $val. ' '; } ? > A keyed array: $stuff[mine] = BMW $stuff[yours] = Lexus $stuff[ours] = house

Using for. . . loop david apple If keys are not supplied, the Xena default is integer keys starting Sue at 0. Using implied keys with foreach. . . loop a[0] = david a[1] = apple a[2] = Xena a[3] = Sue Document 9. 2. (Consecutive. Key. Array. php) An array with keys starting at an <? php integer other than 0 $a = array('david', 'apple', 'Xena', 'Sue'); BMW echo "Using for. . . loop "; Lexus for ($i=0; $i<sizeof($a); $i++) house echo $a[$i]. ' '; A keyed array with consecutive echo "Using implied keys with foreach. . . loop "; character keys. . . foreach ($a as $i => $x) BMW echo 'a['. $i. '] = '. $x. ' '; Lexus house echo "An array with keys starting at an integer other than 0 " ; $neg. Key = array(-1 => 'BMW', 'Lexus', 'house'); for ($i=-1; $i<2; $i++) echo $neg. Key[$i]. ' '; echo 'A keyed array with consecutive character keys. . . '; $stuff = array('a' => 'BMW', 'b' => 'Lexus', 'c' => 'house'); for ($i='a'; $i<sizeof($stuff); $i++) echo $stuff[$i]. ' '; ? >

Document 9. 3. (base_1 Array. php) <? php echo ' A keyed array with indices starting at 1. . . ' ; $a = array(1 => 63. 7, 77. 5, 17, -3); foreach($a as $key => $val) { echo 'a['. $key. '] = '. $val. ' '; } for ($i=1; $i<=sizeof($a); $i++) echo $a[$i]. ' '; ? > A keyed array with indices starting at 1. . . a[1] = 63. 7 a[2] = 77. 5 a[3] = 17 a[4] = -3 63. 7 77. 5 17 -3

Defining a 2 -D array with default integer keys (starting at 0) Document 9. 4. (two-D. php) <? php echo ' A 2 -D array '; $a = array( 0 => array(1, 2, 3, 4), 1 => array(5, 6, 7, 8), 2 => array(9, 10, 11, 12), 3 => array(13, 14, 15, 16), 4 => array(17, 18, 19, 20) ); $n_r=count($a); echo '# rows = '. $n_r. ' '; $n_c=count($a[0]); echo '# columns = '. $n_c. ' '; for ($r=0; $r<$n_r; $r++) { for ($c=0; $c<$n_c; $c++) echo $a[$r][$c]. ' '; echo ' '; } ? > A 2 -D array # rows = 5 # columns = 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

A basic PHP sort still has problems with upper/lower case strings… Document 9. 5. (sort 1. php) <? php // Create and sort an array… $a = array('david', 'apple', ‘Sue', ‘Xena'); echo 'Original array: '; for ($i=0; $i<sizeof($a); $i++) echo $a[$i]. ' '; sort($a); echo 'Sorted array: '; for ($i=0; $i<sizeof($a); $i++) echo $a[$i]. ' '; ? > Original array: david apple Xena Sue Sorted array: Sue Xena apple david …but not with numbers (unlike Java. Script) Document 9. 6. (sort 2. php) <? php $a=array(3. 3, -13, -0. 7, 14. 4); sort($a); for ($i=0; $i<sizeof($a); $i++) echo $a[$i]. ' '; ? > -13 -0. 7 3. 3 14. 4

Fix the sort function by using a user-defined “compare” function. Document 9. 7. (sort 3. php) <? php function compare($x, $y) { return strcasecmp($x, $y); } // Create and sort an array… $a = array('david', 'apple', 'Xena', 'Sue'); echo 'Original array: '; for ($i=0; $i<sizeof($a); $i++) echo $a[$i]. ' '; echo 'Sorted array with user-defined comparisons of elements: ' ; usort($a, "compare"); for ($i=0; $i<sizeof($a); $i++) echo $a[$i]. ' '; ? > Original array: david apple Xena Sue Sorted array with user-defined comparisons of elements: apple david Sue Xena

Stacks, Queues, and Line Crashers n For manipulating these data structures, assume arrays with integer indices starting at 0. Then we can use: array_pop() array_shift() array_push() array_unshift() remove last (newest) element remove first (oldest) element add new element(s) to “new” end of array add new element(s) to “old” end of array

Document 9. 10 (array. Pop. php) <? php $stack = array("orange", "banana", "apple", "lemon"); $fruit=array_pop($stack); print_r($stack); ? > Array ( [0] => orange [1] => banana [2] => apple) Document 9. 11 (array. Push. php) <? php $stack = array("red", "grn"); $n = array_push($stack, "blu", "wh"); print_r($stack); $stack[] = "blk"; printf(" %u ", $n); print_r($stack); printf(" %u ", sizeof($stack)); ? > Array ( [0] => red [1] => grn [2] => blu [3] => wh ) 4 Array ( [0] => red [1] => grn [2] => blu [3] => wh [4] => blk ) 5

Document 9. 12 (array. Shift. php) <? php $queue = array("orange", "banana", "raspberry", "mango"); print_r($queue); $rotten. Fruit = array_shift($queue); echo ' '. $rotten. Fruit; echo ' '. count($queue); ? > Array ( [0] => orange [1] => banana [2] => raspberry [3] => mango ) orange 3 Document 9. 13 (array. Unshift. php) <? php $a = array("orange", "banana", "raspberry", "mango"); print_r($a); array_unshift($a, "papaya", "mangosteen"); echo ' '. count($a). ' '; print_r($a); ? > Array ( [0] => orange [1] => banana [2] => raspberry

The quadratic formula revisited Document 9. 9 a (quadrat_2. htm) <html> Store coefficients in an array. <head> <title>Solving the Quadratic Equation</title> </head> <body> <form method="post" action="quadrat_2. php"> Enter coefficients for ax<sup>2</sup> + bx + c = 0: <br /> a = <input type="text" value="1" name="coeff[0]" /> (must not be 0)<br /> b = <input type="text" value="2" name="coeff[1]" /><br /> c = <input type="text" value="-8" name="coeff[2]" /><br /><input type="submit" value="click to get roots. . . " /> </form> </body> </html>
![Quadratic formula cont Document 9 9 b quadrat2 php php vardumpPOSTcoeff echo Quadratic formula (cont. ) Document 9. 9 b (quadrat_2. php) <? php var_dump($_POST["coeff"]); echo](https://slidetodoc.com/presentation_image_h/22868fdb039756d2cbe52c2dea69807e/image-13.jpg)
Quadratic formula (cont. ) Document 9. 9 b (quadrat_2. php) <? php var_dump($_POST["coeff"]); echo " "; $coefficient. Array=array_keys($_POST["coeff"]); $a= $_POST["coeff"][$coefficient. Array[0]]; $b=$_POST["coeff"][$coefficient. Array[1]]; $c=$_POST["coeff"][$coefficient. Array[2]]; $d = $b*$b - 4. *$a*$c; if ($d == 0) { $r 1 = $b/(2. *$a); $r 2 = "undefined"; } else if ($d < 0) { $r 1 = "undefined"; $r 2 = "undefined"; } else { $r 1 = (-$b + sqrt($b*$b - 4. *$a*$c))/2. /$a; ; $r 2 = (-$b - sqrt($b*$b - 4. *$a*$c))/2. /$a; ; } echo "r 1 = ". $r 1. ", r 2 = ". $r 2; ? > array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(2) "-8" } r 1 = 2, r 2 = -4

Document 9. 10 a (Cloud. Obs. htm) <html> <head> <title>Cloud Observations</title> </head> <body bgcolor="#aaddff"> <h 1>Cloud Observations</h 1> <strong> Cloud Observations </strong>(Select as many cloud types as observed. ) <br /> <form method="post" action="Cloud. Obs. php" /> <table> <tr> <td><strong>High</strong> </td> <td> <input type="checkbox" name="high[]" value="Cirrus" /> Cirrus</td> <td> <input type="checkbox" name="high[]" value="Cirrocumulus" /> Cirrocumulus </td> <td> <input type="checkbox" name="high[]" value="Cirrostratus" /> Cirrostratus </td></tr> <tr> <td colspan="4"><hr noshade color="black" /> </td></tr> <tr> <td> <strong>Middle</strong> </td> <td> <input type="checkbox" name="mid[]" value="Altostratus" /> Altostratus </td> <td> <input type="checkbox" name="mid[]" value="Altocumulus" /> Altocumulus</td></tr> <td colspan="4"><hr noshade color="black" /> </td></tr> <tr> <td> <strong>Low</strong></td> <input type="checkbox" name="low[]" value="Stratus" /> Stratus</td> <td> <input type="checkbox" name="low[]" value="Stratocumulus" /> Stratocumulus</td> <input type="checkbox" name="low[]" value="Cumulus" /> Cumulus </td></tr> <tr> <td colspan="4"><hr noshade color="black" /> </td></tr> <tr> <td> <strong>Rain-Producing </strong> </td> <td> <input type="checkbox" name="rain[]" value="Nimbostratus" /> Nimbostratus</td> <td> <input type="checkbox" name="rain[]" value="Cumulonimbus" /> Cumulonimbus </td></tr> </table> <input type="submit" value="Click to process. . . " /> </form> </body> </html>
![Document 9 10 b Cloud Obs php php high POSThigh PHP builds Document 9. 10 b (Cloud. Obs. php) <? php $high = $_POST["high"]; PHP builds](https://slidetodoc.com/presentation_image_h/22868fdb039756d2cbe52c2dea69807e/image-15.jpg)
Document 9. 10 b (Cloud. Obs. php) <? php $high = $_POST["high"]; PHP builds an array containing $n = count($high); only those boxes which have been echo "For high clouds, you observed "; checked. (Easier than Java. Script!) for ($i=0; $i<$n; $i++) echo $high[$i]. " "; $mid = $_POST["mid"]; $n = count($mid); echo "for mid clouds, you observed "; for ($i=0; $i<$n; $i++) echo $mid[$i]. " "; $low = $_POST["low"]; $n = count($low); echo "for low clouds, you observed "; for ($i=0; $i<$n; $i++) echo $low[$i]. " "; $rain = $_POST["rain"]; $n = count($rain); echo "for precipitating clouds, you observed "; for ($i=0; $i<$n; $i++) For high clouds, you observed echo $rain[$i]. " "; Cirrocumulus ? > Cirrostratus for mid clouds, you observed for low clouds, you observed for precipitating clouds, you observed Cumulonimbus

Write a PHP application that reads scores between 0 and 100 (possibly including both 0 and 100) and creates a histogram array whose elements contain the number of scores between 0 and 9, 10 and 19, etc. The last “box” in the histogram includes scores between 90 and 100. Use a function to generate the histogram. The data file looks like this (integers between 0 and 100): 73 77 86 … 17 18 Your output should look something like this: Number of scores: 39 building histogram. . . size of histogram array = 10 h[0] = 1 h[1] = 5 h[2] = 2 h[3] = 5 h[4] = 5 h[5] = 1 h[6] = 3 h[7] = 3 h[8] = 8 h[9] = 6 # of entries = 39

Write a PHP application that will shuffle a deck of 52 “cards. ” Represent the cards as an array of integers having values from 1 to 52. After the results of shuffling this “deck” are displayed, sort the deck in ascending order and display it again. Document 9. 13 (card. Shuffle. php) <? php $deck = array(); for ($i= 0; $i<52; $i++) { $deck[$i]=$i+1; echo $deck[$i]. " "; } echo " "; for ($i=0; $i<52; $i++) { $j=rand(0, 51); $save=$deck[$i]; $deck[$i]=$deck[$j]; $deck[$j]=$save; } for ($i=0; $i<52; $i++) echo $deck[$i]. " "; echo " "; sort($deck); echo "Resort deck. . . "; for ($i=0; $i<52; $i++) echo $deck[$i]. " "; echo "<br /"; ? >