Home  >  Article  >  Backend Development  >  php each returns the current key-value pair in the array and moves the array pointer one step forward. Example

php each returns the current key-value pair in the array and moves the array pointer one step forward. Example

高洛峰
高洛峰Original
2016-12-30 11:29:211120browse

each function returns the current key/value pair in the array and moves the array pointer one step forward

Basic syntax

array each (array &$array)

After executing each(), the array pointer will stay at the next unit in the array or at the last unit 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:

php each 返回数组中当前的键值对并将数组指针向前移动一步实例

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 The internal pointer moves forward.

Return value:

Returns the key/value pair of the current pointer position in the array 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:

Operation result;

Array
(
[1] => bob
[value] => ; bob
[0] => 0
[key] => 0
)

each function example two:

each() combination list() traverses the array

 'apple',
  'b' => 'banana',
  'c' => 'cranberry'
);
reset($fruit);
while (list($key, $val) = each($fruit)) {
  echo " $key => $val 
"; } ?>

Run result:

a => apple
b => banana
c => cranberry

Thanks for reading , hope it can help everyone, thank you for your support of this site!

For more php each returns the current key-value pair in the array and moves the array pointer forward one step for related articles, please pay attention to the PHP Chinese website!

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