Displaying the contents of an array using a loop : foreach « Statement « PHP






Displaying the contents of an array using a loop

 
<?php
$shapes = array('S' => 'Cylinder',
                'N' => 'Rectangle',
                'A' => 'Sphere',
                'O' => 'Sphere',
                'P' => 'Rectangle');
foreach ($shapes as $key => $value) {
    print "The $key is a $value.<br />";
}
?>
  
  








Related examples in the same category

1.Foreach Demo
2.Looping through an Enumerated Array
3.Array element order and foreach()
4.Iterating through a multidimensional array with foreach()
5.Modifying an array with foreach()
6.Using foreach() to Iterate Through an Array
7.Using foreach() with numeric arrays
8.Iterating Through Object Properties
9.Implementing the traversable Iterator