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 _PHP tutorial

WBOY
Release: 2016-07-21 15:23:31
Original
931 people have browsed it

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 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 combination with list() to iterate over the 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 => Set 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.

http://www.bkjia.com/PHPjc/324470.html

truehttp: //www.bkjia.com/PHPjc/324470.htmlTechArticleeach() definition and usage The each() function generates the key name and sum of the elements pointed to by the current internal pointer of the array An array of key values ​​and moves the internal pointer forward. The returned array contains...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!