PHP Loop Traversal Array 3 methods list (), each (), and while summary _php instance

Source: Internet
Author: User
Tags arrays mixed

①each () function

The each () function needs to pass an array as a parameter, return the key/value pair of the current element in the array, and move the array pointer back to the next element's position. The key/value pair is returned with an array of 4-element associations and index blends, with the key names 0, 1, key, and value respectively. Where the value of key 0 and key is the same, the key name of the array element, and 1 and value contain the values of the array elements. Each () returns false if the internal pointer crosses the end of the array. The use of each () function is shown below:

Copy Code code as follows:

<?php
$contact = Array ("ID" => 1, "name" => "High", "Company" => "a Company", "Address" => "Beijing",);
$id = each ($contact); Returns the key/value pair of the first element in the array $contact, which is an array with 4 elements
Print_r ($id); Output array $id:array ([1]=>1,[value]=>1,[0]=>id,[key]=>id)

$name = each ($contact); Returns the key/value pair of the second element in the array $contact, which is an array with 4 elements
Print_r ($name); Output Array ([1]=> high some,[value]=> high,[0]=> name,[key]=> name)

$company = each ($contact);
Print_r ($company); Output Array ([1]=>a company, [Value]=>a,[0]=> company,[key]=> Company)

$address = each ($contact);
Print_r ($address); Output Array ([1]=> Beijing,[value]=>,[0]=> address,[key]=> address)

$no = each ($contact);
Var_dump ($no); output bool (FALSE)
?>

②list () function

This is not a real function, but the language structure of PHP. The list () assigns a set of variables in one step, assigning the values in the array to some variables. The list () can only be used for arrays of numeric indices and assumes that the array index starts at 0. The syntax format looks like this:

Copy Code code as follows:

List (mixed varname,mixed ...) = Array_expression

The list () statement differs greatly from other functions in that it does not directly receive an array as an argument. Instead, the value of each element in the array is assigned to each parameter in the list () function by the "=" operator. The list () function converts each parameter in it to a variable that can be used directly in the script. The use of the following methods:
Copy Code code as follows:

<?php
$info = Array (' coffee ', ' brown ', ' caffeine ');
List ($drink, $color, $power) = $info;

List ($drink,, $power) = $info; The value of the variable is the first and third value in the array

List (,, $power) = $info; The value of the variable is the value of the third element in the array
?>

Using the previous example to understand the use of the list () function, combine each () function with the list () function. The code looks like this:

Copy Code code as follows:

<?php
$contact = Array ("ID" => 1, "name" => "High", "Company" => "a Company", "Address" => "Beijing",);

List ($key, $value) = each ($contact);
echo "$key => $value"; Output variables $key and $value, with "=>" split in the middle
?>

③while Loop Traversal Array

The use of each () and list () statement described earlier is not difficult to understand if you use a while loop to traverse an array. The syntax format used is as follows:

Copy Code code as follows:

while (the list ($key, $value) = each (array_expression)) {
Circulation body
}

The format of this consortium traverses the given array_expression array. In each loop of the while () statement, the each () statement assigns the key of the current array element to the first parameter variable $key of the list () function. And the value in the current array element, assigns the second parameter variable $value in the list () function, and after each () statement is executed, the pointer inside the array is moved backward, so the next while () statement loops, the key/value pairs of the next element in the array are obtained. Until the end of the array each () statement returns the False,while () statement stops the loop, ending the traversal of the array.

Copy Code code as follows:

<?php
$contact = Array (
"ID" => 1,
"Name" => "Gao MoU",
"Company" => "a Company",
"Address" => "Beijing",
"Telephone" => "(010) 98765432",
"EMAIL" => "gao@brophp.com",
);

Output information for each element in an array as an HTML list
Echo ' <dl> A contact information: ';

while (the list ($key, $value) = each ($contact)) {
echo "<dd> $key: $value </dd>";
}

Echo ' </dl> ';
?>

The

can also be a nested traversal of multidimensional arrays in the same way as yo on. Although the result of the while traversal array is the same as the Freach statement, there are differences between the two methods. After using the while statement to traverse an array, the each () statement already points the incoming array parameter internal pointer to the end of the array. When you use the while statement to traverse the same array, the array pointer is already at the end of the array, and the False,while statement is returned directly from each () statement without a loop being executed. The reset () function is called again before the while statement is executed, and the array pointer is assigned the first element. The foreach statement automatically resets the pointer position of the array, and when foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call the Reset () function before the Foreach loop.

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.