Learn PHP

array element loop

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.


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

Function Description
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.

			 $authors = array( "mysql", "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"; 

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

			// Create a sparse indexed array 
			$authors = array( 0 =>  "mysql", 1 =>  "php", 2 =>  "css", 47 => "html" ); 
			echo end( $authors ); // Displays "HTML"   

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 Index Element value
0 The current element's key
"key" The current element's key
1 The 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.

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

PHP Tutorials






Echo and Print Statement

Data Types



Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o