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

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.