The phpeach function is used to obtain the key-value pairs of an array and move the array pointer one step forward. the each function is often used with the list function to traverse the array. This article introduces the basic usage of each. if you need it, refer to the each function to return the current key/value pair in the array and move the array pointer one step forward.
Basic syntax
Array each (array & $ array)
After executing each (), the array pointer will stay in the next unit of the array or stay in the last unit when it comes to the end of the array. If you want to use each to traverse the array, you must use reset ().
Parameter introduction:
Parameters |
Description |
Array |
Required. Specifies the array to be used. |
The each () function generates an array consisting of the key and key values of the elements 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 the array and moves the array pointer forward. The key-value pair is returned as an array of four units, with the key names 0, 1, key, and value. Unit 0 and key contain the key name of the array unit, and 1 and value contain data. If the internal pointer crosses the end of the array, each () returns FALSE.
Example 1 of the each function:
<?php$foo = array( "bob", "fred", "jussi", "jouni", "egon", "marliese");$bar = each($foo);print_r($bar);?>
Running result;
Array
(
[1] => bob
[Value] => bob
[0] => 0
[Key] => 0
)
Example 2 of the each function:
Use each () and list () to traverse the array
<?php$fruit = array( 'a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');reset($fruit);while (list($key, $val) = each($fruit)) { echo " $key => $val
";}?>
Running result:
A => apple
B => banana
C => cranberry
Thank you for reading this article. I hope it will help you. thank you for your support for this site!