Home >>PHP Array Functions >PHP each() Function

PHP each() Function

PHP each() Function

PHP each() function is used to get the current element key-value pair of the given input array to which the internal pointer is currently pointing to. After returning the key and value of the current element the internal pointer value is incremented by one in the array. It accepts only a single parameter $array that is the given input array

Syntax:

each($array);

Parameter Values

Parameter Description
array array This is a required parameter. This parameter defines the array to use.

Here is an example of each() function in PHP:

<html>
<body>
<pre>
<?php
$x = array("a","b","c","d","e","f","g","h");
print_r (each($x));
?>
</pre>
</body>
</html>
Output:
Array
(
    [1] => a
    [value] => a
    [0] => 0
    [key] => 0
)	

Example 2:

<html>
<body>
<?php
$x = array("a","b","c","d","e","f","g","h");
reset($x);
while (list($key, $val) = each($x))
  {
  echo "$key => $val<br>";
  }
?>
</body>
</html>
Output:
0 => a
1 => b
2 => c
3 => d
4 => e
5 => f
6 => g
7 => h

No Sidebar ads