PHP each() function

PHP current() function
PHP end() function

In this article, you will learn how to get the current element key and value and move the internal pointer to the next element of the array to do the same.

Note: This function is deprecated in PHP 7.2

You will find all the related methods to each method in the last part of the article.

What is the syntax of the each function in PHP?

each(array)
ParameterDescription
arrayRequired. Specifies the array to use
PHP each function

Examples of each function

Example 1. Get the current element key and value and move the array pointer to the next element.

<?php
$member= array("Jawad", "Ahmad", "Sumerina", "ACS");
print_r (each($member));
?>

Example 2. Get key and value of current element and move the pointer to the next element, also print the whole array.

<?php
$member= array("Jawad", "Ahmad", "Sumerina", "ACS");
reset($member);

while (list($key, $val) = each($member))
  {
  echo "$key => $val . " - " ";
  }
?>

Example 3. In this example, you will find all the methods related to the current method category. These methods work with the internal pointer of the arrays.

<?php
$members= array("Jawad", "Ahmad", "Sumerina", "ACS");

// Current element: Jawad
echo current($people) . "<br>";

// Next element of Jawad: Ahmad
echo next($people) . "<br>"; 

// Now the current element: Ahmad
echo current($people) . "<br>"; 

// Previous element of Ahmad: Jawad
echo prev($people) . "<br>"; 

// Last element: ACS
echo end($people) . "<br>"; 

// Previous element of ACS: Sumerina
echo prev($people) . "<br>"; 

// Current element: Sumerina
echo current($people) . "<br>"; 

// Move the internal pointer to the first element of the array:Jawad
echo reset($people) . "<br>"; 

// Next element of Jawad: Ahmad
echo next($people) . "<br>"; 

// Get key and value of Ahmad (current element) then moves the internal pointer forward
print_r (each($members));
?>
PHP current() function
PHP end() function
en English
X
Scroll to Top