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

Source: Internet
Author: User
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 each (), the array pointer stays in the next cell in the array, or when the end of the array is encountered, the last cell. If you want to iterate through the array again, you must use Reset ().

Parameter description:

The each () function generates an array of the key names and key values of the elements pointed to by the current internal pointer to the array, and moves the internal pointer forward.

return value:

Returns the key/value pair of the current pointer position in an array and moves the array pointer forward. A key-value pair is returned as an array of four cells with the key name 0,1,key and value. Unit 0 and key contain the key name of the array cell, and 1 and value contain the data. If the internal pointer crosses the end of the array, each () returns FALSE.

Each function instance one:

<?php$foo = Array (  "Bob", "  Fred", "  Jussi",  "Jouni",  "Egon",  "Marliese"); $bar = each ($ Foo);p Rint_r ($bar);? >

Operation result;

Array
(
[1] = Bob
[Value] = Bob
[0] = 0
[Key] = 0
)

Each function instance two:

Each () iterates through the array with the list ()

<?php$fruit = Array (  ' a ' = = ' Apple ',  ' b ' = ' banana ',  ' c ' = ' cranberry '); reset ($fruit); while ( List ($key, $val) = each ($fruit)) {  echo "$key + = $val <br/>";}? >

Operation Result:

A = Apple
b = Banana
c = Cranberry

Thank you for reading, hope to help everyone, thank you for the support of this site!

Related Article

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.