PHP arrays can be indexed or associative. Indexed arrays are similar to conventional programming language arrays while associative arrays are like dictionaries or maps where elements can be accessed by keys. Array elements can be of any type and arrays can be heterogeneous. PHP provides many functions for manipulating arrays like count(), sizeof(), array_slice(), in_array(), sorting functions, and more.
5. Listing array element : for PHP Array $animals = array("dog", "cat", "fish"); for ($i = 0; $i < count($animals); $i++) { echo $i . "-th animal is a $animals[$i]."; } 0-th animal is a dog. 1-th animal is a cat. 2-th animal is a fish.
6. Listing Array Elements: foreach PHP Array $animals = array("dog", "cat", "fish"); foreach ($animals as $animal) echo "$animal"; } dog cat fish
7. while and each PHP Array $animals = array( "dog“ => 15,"cat“ => 8, "fish“ => 2); while ($item = each($animals)) print "weight of " . $item["key"] . " is " . $item["value"] . “."; weight of dog is 15. weight of cat is 8. weight of fish is 2.
8. each and list PHP Array $animals = array( "dog“ => 15, "cat“ => 8, "fish“ => 2); while (list($key, $value) = each($animals)) print "weight of $key is $value."; weight of dog is 15. weight of cat is 8. weight of fish is 2.
10. Nested Loops PHP Array for ($i=0; $i < count($books); $i++) { print "$i-th book is:"; while ( list($key, $value) = each($books[$i]) ) print “ $key: $value"; print ""; } 0-th book is: title: A author: X 1-th book is: title: B author: Y price: 25
11. String as an Array PHP Array $myString = "My chars"; echo "$myString"; echo "$myString[1]"; My chars y