Download presentation
Presentation is loading. Please wait.
Published byMason Gonzalez Modified over 11 years ago
2
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays. Entire form arguments in one array. Nice container for sorting and counting
3
$array[index] = value; $state_location[San Diego] = CA; $state = $state_location[San Diego]; $my_array[1] = First Thing; $my_array[2] = Second Thing; $vegetables[] = carrot; $vegetables[] = lettuce; $fruits = array( red => apple, green => pear ); echo $fruits[red]; // prints apple
4
» is_array($variable) Returns true if variable is an array » count($array) Returns number of non-empty elements » in_array(value, $array) Returns true if value found in array » isset($array[key]) Returns true if key is valid for the array » unset($array[key]) Deletes a key value pair from the array
5
» To execute commands on each item in an array, use the foreach loop » Foreach runs as many times as there are key/values in an array » Reference: php.net - foreach php.net
6
$my_array[1] = First Thing; $my_array[2] = Second Thing; foreach($my_array as $value){ echo $value echo ' '; } OUTPUT: First Thing Second Thing
7
» Now its your turn. 1.We will use the foreach loop to delete one or many comments from the admin_comments page 2.Save the files in the Blog folder. 3.Confirm the pages work in a browser. 4.Revel in your programming glory.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.