Friday, October 10, 2025
HomeLanguagesPHP | each() Function

PHP | each() Function

The each() function is an inbuilt function in PHP which basically returns an array with four elements, two elements (1 and Value) for the element value, and two elements (0 and Key) for the element key and moves the cursor forward.
Syntax:

each(array)

Parameters:

Array:
It specifies the array which is being 
taken as input and used for each() function.

Return Values:

It returns the current element key and value which are an array with four elements out of
which two elements (1 and Value) for the element value, and two elements (0 and Key) for 
the element key.It returns FALSE if there are no array elements.

PHP Version:
4+
Let’s see PHP program:
Program 1:




<?php
// PHP program to demonstrate working of each()
// for simple array.
  
// input array contain some elements inside.
$a = array("Ram", "Shita", "Geeta");
  
// each() function return in the array with four elements
// Two elements (1 and Value) for the element value which 
// are Ram, and two elements (0 and Key) for the element
// key which are not given here so output is zero.
print_r (each($a));
  
// Next set is printed as cursor is moved
print_r (each($a));
?>


Output:

Array
(
    [1] => Ram
    [value] => Ram
    [0] => 0
    [key] => 0
)
Array
(
    [1] => Shita
    [value] => Shita
    [0] => 1
    [key] => 1
)

Program 2:




<?php
// PHP program to demonstrate working of each()
// for associative array.
  
$a = array("101"=>"Ram", "105"=>"Geeta", "104"=>"Geek");
  
// each() function return in the array with four elements
// Two elements (1 and Value) for the element value which 
// are Ram, and two  elements (0 and Key) for the element 
// key which are Boy.
print_r (each($a));
  
// Next set is printed as cursor is moved
print_r (each($a));
?>


Output:

Array
(
    [1] => Ram
    [value] => Ram
    [0] => 101
    [key] => 101
)
Array
(
    [1] => Geeta
    [value] => Geeta
    [0] => 105
    [key] => 105
)

Reference:
http://php.net/manual/en/function.each.php

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS