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

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

墨辰丷
Release: 2023-03-28 14:02:02
Original
2001 people have browsed it

php The each function is used to obtain the key-value pairs of the array and move the array pointer forward one step. The each function is often used in conjunction with list to traverse the array. This article introduces you to the basic usage of each. Friends who need it can refer to it

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

Basic syntax

array each (array &$array)

After executing each(), the array pointer will stay at the next unit in the array or when touching Stay at the last cell when reaching the end of the array. If you want to use each to iterate through the array again, you must use reset().

Parameter introduction:


Parameter 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 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:

<?php
$foo = array(
  "bob",
  "fred",
  "jussi",
  "jouni",
  "egon",
  "marliese"
);
$bar = each($foo);
print_r($bar);
?>
Copy after login

Run result;

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

each function example two:

each() combined with list() to traverse the array

<?php
$fruit = array(
  &#39;a&#39; => &#39;apple&#39;,
  &#39;b&#39; => &#39;banana&#39;,
  &#39;c&#39; => &#39;cranberry&#39;
);
reset($fruit);
while (list($key, $val) = each($fruit)) {
  echo " $key => $val <br/>";
}
?>
Copy after login

Run result:

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

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP method to implement email sending instance based on SMTP protocol

PHP implements the method of sorting multi-dimensional arrays according to a certain key value

phpImplements uploading excel tables and obtaining data

The above is the detailed content of php each returns the current key-value pair in the array and moves the array pointer forward one step. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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