PHP Traversal Array Method foreach

Source: Internet
Author: User
Tags unpack

foreach

http://php.net/manual/zh/control-structures.foreach.php

(PHP 4, PHP 5)

The foreach syntax structure provides an easy way to iterate through an array. foreach can only be applied to arrays and objects, if you try to apply a variable to another data type, or an uninitialized variable will emit an error message. There are two kinds of syntax:

foreach (array_expression as $value)    Statementforeach (array_expression as $key = $value)    statement

The first format iterates through the given array of array_expression . In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).

The second format does the same thing, except that the key name of the current cell is assigned to the variable $keyin each loop.

You can also customize the traversal of objects.

Note:

When foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the foreach loop .

Because foreach relies on an internal array pointer, modifying its value in a loop can cause unexpected behavior.

You can easily modify the elements of an array by adding & to it before $value . This method assigns a value to a reference instead of copying a value.

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // 最后取消掉引用
?>

A reference to a $value is available only if the array being traversed can be referenced (for example, a variable). The following code does not work:

<?php
foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
}

?>
Warning

The $value Reference of the last element of the array remains after the foreach loop. It is recommended to use unset () to destroy it.

Note:

foreach does not support the ability to suppress error messages with "@".

The user may notice that the following code functions exactly the same:

<?php
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br>\n";
}

foreach ($arr as $value) {
    echo "Value: $value<br />\n";
}
?>

The following code functions are identical:

<?php
$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value<br />\n";
}

foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
?>

More examples of demonstration usage:

<?php
/* Foreach Example 1:value only */

$a = Array (1, 2, 3, 17);

foreach ($a as $v) {
echo "Current value of \ $a: $v. \ n";
}

/* foreach example 2:value (with it manual access notation printed for illustration) */

$a = Array (1, 2, 3, 17);

$i = 0; /* For illustrative purposes only */

foreach ($a as $v) {
echo "\ $a [$i] = $v. \ n";
$i + +;
}

/* Foreach example 3:key and value */

$a = Array (
"One" = 1,
"Both" = 2,
"Three" = 3,
"Seventeen" = 17
);

foreach ($a as $k = = $v) {
echo "\ $a [$k] = $v. \ n";
}

/* foreach Example 4:multi-dimensional arrays */
$a = array ();
$a [0][0] = "a";
$a [0][1] = "B";
$a [1][0] = "Y";
$a [1][1] = "Z";

foreach ($a as $v 1) {
foreach ($v 1 as $v 2) {
echo "$v 2\n";
}
}

/* foreach Example 5:dynamic arrays */

foreach (Array (1, 2, 3, 4, 5) as $v) {
echo "$v \ n";
}
?>
Unpack nested arrays with list ()

(PHP 5 >= 5.5.0)

PHP 5.5 adds the ability to iterate through an array of arrays and unpack the nested array into a loop variable by simply supplying the list () as a value.

For example:

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b)) {
    // $a contains the first element of the nested array,
    // and $b contains the second element.
    echo "A: $a; B: $b\n";
}
?>

The above routines will output:

a:1; B:2a:3; B:4

The cells in list () can be less than the nested arrays, and the extra array cells are ignored:

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a)) {
    // Note that there is no $b here.
    echo "$a\n";
}
?>

The above routines will output:

13

If more cells are listed in list () than nested arrays, a message-level error message is emitted:

<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b, $c)) {
    echo "A: $a; B: $b; C: $c\n";
}
?>

The above routines will output:

PHP Traversal Array Method foreach

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.