English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP each() Function Usage and Example

PHP Array Function Manual

The each() function returns the current key/value pair from the array and moves the array pointer one step forward

Syntax

each ($array);

Definition and Usage

The each() function returns the current key-value pair from the array array and advances the array cursor. The pair is returned in the form of a four-element array, with keys 0,1Key and value. Element 0 and key contain the name of the array element, element1and value contain data.

From PHP 7.2Starting from version 7.0, this feature has been deprecated. It is strongly recommended not to use this feature anymore.

Parameter

Serial NumberParameters and Description
1

array (required)

It specifies an array

Return Value

It returns the current key-value pair from the array.

Online Example

<?php
   $transport = array('foot', 'bike', 'car', 'plane');
   $key_value = each($transport);
   
   print_r($key_value);
   print "<br />";
   
   $key_value = each($transport);
   print_r($key_value);
   print "<br />";
   
   $key_value = each($transport);
   print_r($key_value);
?>
Test and See‹/›

Output Result:

Array ( [1] => foot [value] => foot [0] => 0 [key] => 0 )
Array ( [1] => bike [value] => bike [0] => 1 [key] => 1 )
Array ( [1] => car [value] => car [0] => 2 [key] => 2 )

   PHP Array Function Manual