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

Source: Internet
Author: User

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

Parameter introduction:

Parameters Description
Array Necessary. Specify the array to use.

The each () function generates an array of key names and key values of the elements that the array's current internal pointer points to, and moves the internal pointer forward.

return value:

Returns the key/value pair of the current pointer position in array arrays and moves the array pointer forward. A key-value pair is returned as an array of four cells, with the key named 0,1,key and value. The unit 0 and key contain the key names of the array cells, and 1 and value contain the data. Each () returns FALSE if the internal pointer crosses the end of the array.

Each function instance one:

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

Operation results;

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

instance of each function two:

Each () walks the array with the list ()

<?php
$fruit = Array (
  ' a ' => ' apple ',
  ' B ' => ' banana ',
  ' C ' => ' cranberry '
);
Reset ($fruit);
while (the list ($key, $val) = each ($fruit)) {
  echo "$key => $val <br/>"
;
> 

Run Result:

A => apple
b => Banana
C => Cranberry

Thank you for reading, I hope to 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.