PHP Tutorial - PHP Array Element Loop Through






PHP provides us with a suite of array access functions that we can use to step through each element in an array, regardless of how the elements are indexed.

PHP maintains an internal pointer to the array elements. This pointer initially points to the first element, but you can move the pointer forward and backward through the array.

Functions

To manipulate the pointer and access the elements that it points to, use the following functions:

FunctionDescription
current()Get pointer of the current element, without changing the pointer position.
key()Get the index of the current element pointed to by the pointer, without changing the pointer position.
next()Move the pointer forward to the next element, and returns that element's value.
prev()Move the pointer backward to the previous element, and returns that element's value.
end()Move the pointer to the last element in the array, and returns that element's value.
reset()Move the pointer to the first element in the array, and returns that element's value.

Each of these functions takes just one argument, the array, and returns the required element's value or index, or false if an element couldn't be found.

If you use next() when the pointer is at the end of the array, or you use current() on an empty array. They will return false.





Example

Step through array

<?php 
 $authors = array( "Java", "PHP", "CSS", "HTML" ); 
 echo "The array: " . print_r( $authors, true ) . "\n"; 
 echo "The current element is: " . current( $authors ) . "\n"; 
 echo "The next element is: " . next( $authors ) . "\n"; 
 echo "...and its index is: " . key( $authors ) . "\n"; 
 echo "The next element is: " . next( $authors ) . "\n"; 
 echo "The previous element is: " . prev( $authors ) . "\n"; 
 echo "The first element is: " . reset( $authors ) . "\n"; 
 echo "The last element is: " . end( $authors ) . "\n"; 
 echo "The previous element is: " . prev( $authors ) . "\n"; 
 ?>  

The code above generates the following result.





Example 2

Retrieve the last element of the array without knowing how it's indexed.

<?PHP
// Create a sparse indexed array 
$authors = array( 0 =>  "Java", 1 =>  "PHP", 2 =>  "CSS", 47 => "HTML" ); 
echo end( $authors ); // Displays "HTML"   
?>

The code above generates the following result.

Each function

each() returns a four-element array rather than a value. This array contains both the key of the current element, as well as its value. If an element couldn't be retrieved because the pointer has reached the end of the array or because the array is empty, each() returns false.

This makes it easy to tell if each() has retrieved an element with the value of false in which case it returns the four-element array or if it couldn't retrieve an element at all, in which case it returns false.

The four-element array that each() has elements with both numeric and string indices, as follows:

Element IndexElement Value
0The current element's key
"key"The current element's key
1The current element's value
"value"The current element's value

In other words, you can use an index of either 0 or "key" to access the current element's key, or an index of 1 or "value" to access its value. For example:

<?PHP
    $myBook = array( "title" =>  "Learn PHP from java2s.com", 
                    "author" =>  "java2s.com", 
                    "pubYear" =>  2000 ); 

    $element = each( $myBook ); 
    echo "Key: " . $element[0] . "\n"; 
    echo "Value: " . $element[1] . "\n"; 
    echo "Key: " . $element["key"] . "\n"; 
    echo "Value: " . $element["value"] . "\n";   
?>         

The code above generates the following result.

Example 3

Use each() to retrieve an array element with a value of false:

<?PHP
         $myArray = array( false ); 
         $element = each( $myArray ); 
         $key = $element["key"]; // $key now equals 0 
         $val = $element["value"]; // $val now equals false   
?>

Because each() both returns the current array element and advances the array pointer, it's easy to use it in a while loop to move through all the elements of an array. The following example works through the $myBook array, returning each element's key and value as it goes.

<?php 
  $myBook = array( "title" =>  "Learn PHP from java2s.com", 
                   "author" =>  "java2s.com", 
                   "pubYear" =>  2000 ); 

   while ( $element = each( $myBook ) ) { 
            echo " < dt > $element[0] < /dt > "; 
            echo " < dd > $element[1] < /dd > "; 
   } 
?>  

The code above generates the following result.