Using foreach() to Iterate Through an Array : foreach « Statement « PHP






Using foreach() to Iterate Through an Array

 
<?php

    $myarray = array('php', 'is', 'cool');

    foreach($myarray as $key => $val) {
      echo "The value of index $key is: $val<BR>";
    }

    foreach($myarray as $val) {
       echo "Value: $val<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() with numeric arrays
7.Displaying the contents of an array using a loop
8.Iterating Through Object Properties
9.Implementing the traversable Iterator