Home >Backend Development >PHP Tutorial >php each returns the current key-value pair in the array and moves the array pointer forward one step instance_php instance
each function returns the current key/value pair in the array and moves the array pointer forward one step
Basic Grammar
array each ( array &$array )
After executing each(), the array pointer will stay at the next cell in the array or at the last cell when the end of the array is reached. If you want to use each to iterate through the array again, you must use reset().
Parameter introduction:
Parameters | Description |
---|---|
array | Required. Specifies the array to use. |
each() function generates an array consisting of the key name and key value of the element pointed to by the current internal pointer of the array, and moves the internal pointer forward.
Return value:
Returns the key/value pair of the current pointer position in array and moves the array pointer forward. Key-value pairs are returned as a four-element array with key names 0, 1, key, and value. Cells 0 and key contain the key names of the array cells, and 1 and value contain the data. If the internal pointer is past the end of the array, each() returns FALSE.
each function example one:
<?php $foo = array( "bob", "fred", "jussi", "jouni", "egon", "marliese" ); $bar = each($foo); print_r($bar); ?>
Operation results;
Array
(
[1] => bob
[value] => bob
[0] => 0
[key] => 0
)
each function example two:
each() combined with list() traverses the array
<?php $fruit = array( 'a' => 'apple', 'b' => 'banana', 'c' => 'cranberry' ); reset($fruit); while (list($key, $val) = each($fruit)) { echo " $key => $val <br/>"; } ?>
Run result:
a => apple
b => banana
c => cranberry
Thanks for reading, I hope it can help you, thank you for your support of this site!