Home  >  Article  >  Backend Development  >  PHP array PHP array function sequence each - Get the key name and key value of the element pointed to by the current internal pointer of the array, and move the pointer to the next bit

PHP array PHP array function sequence each - Get the key name and key value of the element pointed to by the current internal pointer of the array, and move the pointer to the next bit

WBOY
WBOYOriginal
2016-07-29 08:47:01887browse

each() definition and usage
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.
The returned array includes four elements: 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 exceeds the range of the array, this function will return FALSE.
Syntax
each(array) Parameter Description
array Required. Specifies the array to use.
Example 1

Copy the code The code is as follows:


$people = array("Peter", "Joe", "Glenn", "Cleveland");
print_r (each($ people));
?>


Output:
Array ( [1] => Peter [value] => Peter [0] => 0 [key] => 0 ) Example 2
each( ) is often used in conjunction with list() to traverse an array. This example is similar to the previous example, but the entire array is output in a loop:

Copy code The code is as follows:


$people = array("Peter", "Joe", "Glenn", "Cleveland");
reset($people);
while (list($key, $val) = each($people))
{
echo "$key => $val
";
}
?>


Output:
0 => Peter
1 => Joe
2 => Glenn
3 => Cleveland example explanation
Because assigning one array to another array will reset The original array pointer, so in the above example if we assign $people to another variable inside the loop, it will cause an infinite loop.

The above introduces the PHP array PHP array function sequence each - obtains the key name and key value of the element pointed to by the current internal pointer of the array, and moves the pointer to the next position, including PHP array content. I hope you are interested in PHP tutorials. Friends help.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn