PHP 8.3.0 RC 6 available for testing

foreach

(PHP 4, PHP 5, PHP 7, PHP 8)

foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。有两种语法:

 
foreach (iterable_expression as $value)
    statement
foreach (iterable_expression as $key => $value)
    statement

第一种格式遍历给定的 iterable_expression 迭代器。每次循环中,当前单元的值被赋给 $value

第二种格式做同样的事,只除了当前单元的键名也会在每次循环中被赋给变量 $key

注意 foreach 不会修改类似 current()key() 函数所使用的数组内部指针。

还能够自定义遍历对象

可以很容易地通过在 $value 之前加上 & 来修改数组的元素。此方法将以引用赋值而不是拷贝一个值。

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

警告

数组最后一个元素的 $value 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁。 否则你会遇到下面的情况:

<?php
$arr
= array(1, 2, 3, 4);
foreach (
$arr as &$value) {
$value = $value * 2;
}
// 现在 $arr 是 array(2, 4, 6, 8)

// 未使用 unset($value) 时,$value 仍然引用到最后一项 $arr[3]

foreach ($arr as $key => $value) {
// $arr[3] 会被 $arr 的每一项值更新掉…
echo "{$key} => {$value} ";
print_r($arr);
}
// 直到最终倒数第二个值被复制到最后一个值

// output:
// 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 )
// 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 )
// 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
// 3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
?>

可以通过引用来遍历数组常量的值:

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

注意:

foreach 不支持用 “@” 来抑制错误信息的能力。

示范用法的更多例子:

<?php
/* foreach 示例 1:仅 value */

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

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

/* foreach 示例 2:value (打印手动访问的符号以供说明) */

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

$i = 0; /* 仅供说明 */

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

/* foreach 示例 3:key 和 value */

$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);

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

/* foreach 示例 4:多维数组 */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach (
$a as $v1) {
foreach (
$v1 as $v2) {
echo
"$v2\n";
}
}

/* foreach 示例 5:动态数组 */

foreach (array(1, 2, 3, 4, 5) as $v) {
echo
"$v\n";
}
?>

用 list() 给嵌套的数组解包

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

可以遍历一个数组的数组并且把嵌套的数组解包到循环变量中,只需将 list() 作为值提供。

例如:

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

foreach (
$array as list($a, $b)) {
// $a 包含嵌套数组的第一个元素,
// $b 包含嵌套数组的第二个元素。
echo "A: $a; B: $b\n";
}
?>

以上示例会输出:

 
A: 1; B: 2
A: 3; B: 4

list() 中的单元可以少于嵌套数组的,此时多出来的数组单元将被忽略:

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

foreach (
$array as list($a)) {
// 注意这里没有 $b。
echo "$a\n";
}
?>

以上示例会输出:

 
1
3

如果 list() 中列出的单元多于嵌套数组则会发出一条消息级别的错误信息:

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

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

以上示例会输出:

 

Notice: Undefined offset: 2 in example.php on line 7
A: 1; B: 2; C: 

Notice: Undefined offset: 2 in example.php on line 7
A: 3; B: 4; C: 

add a note

User Contributed Notes 3 notes

up
21
Okafor Chiagozie
1 year ago
An easier way to unpack nested array elements

$array = [
[1, 2],
[3, 4],
];

foreach ($array as [$a, $b]) {
echo "A: $a; B: $b\n";
}
up
1
Sanusi Hassan
1 year ago
destructure array elements

you can unpac nested array elements using the following

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

foreach (
$array as $v) {
[
$a, $b] = $v;
echo
"A: $a; B: $b\n";
}
?>
up
-53
jon at pearkins dot com
1 year ago
For an array, foreach presents the elements of the array in the order that they were added to the array. For example, if you need to have the elements presented in ascending order by key, use the ksort function to rearrange the elements of the array in key sequence.

This behavior is a result of the way that PHP arrays are stored. foreach merely presents the elements of an array in the order that they are stored within the array.
To Top