PHP  foreach  loop statement

the foreach loop provides an easy way to iterate over arrays.

the foreach loop works only on arrays and objects.

the foreach loop faster than while and for loops, when iterate over arrays.

syntax:

         foreach ( $array  as  $value){

                                                   // code put here 
                                              }

or

         foreach ( $array  as  $key => $value){

                                                             // code put here 
                                                         }

the $value variable hold the current element of the array which indicate by array pointer.

    <?php
                 
         $array = array("sandeep", "ram", "shyam", "bill gate", "carlos", "steve");
                 
         $index = 0;
                 
               foreach( $array as $value){
                   
                                   echo  "array  [" . $index++ . "] value    $value";
                                   
                                   echo  "<br/>";
                                   
                                }
    ?>

when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array.

when foreach loop execution start, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

   <?php
      
$array = array( 0 => "seteve jobes",

                     1 => "slim karlos",
                                            
                     2 => "warren buffet",
                                            
                     3 => "bill gate",
                                            
                     4 => "foreach loop",
                                            
                     5 => "all images you can put as a images");
                                            
    foreach( $array as  $key => $value){
                          
                                         echo  "array  key = {$key}  value  = {$value}";
                                   
                                         echo  "<br/>";
                                           
                                     } 
    ?>

the following example demonstrates a foreach loop that will access multi-dimensional arrays:

 <?php      
          $array = array();
               
                                 $array[0][0] = "one";
                               
                                 $array[0][1] = "two";
                               
                                 $array[0][2] = "three";  //array initialization statements
                               
                                 $array[1][0] = "four";
                               
                                 $array[1][1] = "five";
                               
                                 $array[1][2] = "six";
                               
                                 $array[2][0] = "seven";
                               
                                 $array[2][1] = "eight";
                               
                                 $array[2][2] = "nine";
                                
 foreach( $array   as   $keyi => $valuef ){
        
            foreach( $valuef  as  $keyj => $values ){
            
      echo  "array  key [". $keyi . "]" . "[" . $keyj . "] " . " value = " . $values ;                                   
                                   
      echo  "<br/>";  
      
   }
   
 }
 
 ?>