2015-05-27
(PHP 4, PHP 5)
The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:
foreach (array_expression as $value) statementforeach (array_expression as $key => $value) statement
The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).
The second form will additionally assign the current element's key to the $key variable on each iteration.
It is possible to customize object iteration.
Note:
When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
As foreach relies on the internal array pointer, changing it within the loop may lead to unexpected behavior.
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won't work:
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>
WarningReference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().
Note:
foreach does not support the ability to suppress error messages using '@'.
You may have noticed that the following are functionally identical:
$arr = array("one", "two", "three");
reset($arr);
while (list(, $value) = each($arr)) {
echo "Value: $value
/n";
}
foreach ($arr as $value) {
echo "Value: $value
/n";
}
?>
The following are also functionally identical:
$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each($arr)) {
echo "Key: $key; Value: $value
/n";
}
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value
/n";
}
?>
Some more examples to demonstrate usage:
/* 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 its 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,
"two" => 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 $v1) {
foreach ($v1 as $v2) {
echo "$v2/n";
}
}
/* foreach example 5: dynamic arrays */
foreach (array(1, 2, 3, 4, 5) as $v) {
echo "$v/n";
}
?>
(PHP 5 >= 5.5.0)
PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value.
For example:
$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 example will output:
A: 1; B: 2A: 3; B: 4
You can provide fewer elements in the list() than there are in the nested array, in which case the leftover array values will be ignored:
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list($a)) {
// Note that there is no $b here.
echo "$a/n";
}
?>
The above example will output:
13
A notice will be generated if there aren't enough array elements to fill the list():
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list($a, $b, $c)) {
echo "A: $a; B: $b; C: $c/n";
}
?>
The above example will output:
Notice: Undefined offset: 2 in example.php on line 7A: 1; B: 2; C: Notice: Undefined offset: 2 in example.php on line 7A: 3; B: 4; C:
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
6
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
PHP中include和require区别之我见
09-05
常用dos命令及语法
2014-09-27
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
java中的info是什么意思
2022-03-24
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
学ug编程如何快速入门?
2022-03-17
PHP数组函数array
2014-09-05
永夜降临复苏游戏下载v3.5.7 安卓最新版
卡牌对战 537.6M
下载创世战车手游下载v1.45.1.98037 安卓版
射击枪战 2.36G
下载dokkan七龙珠爆裂激战国际版手游下载v5.28.6 安卓版
动作闯关 97.6M
下载这就是江湖官方版下载v12.6.6 安卓最新版本
卡牌对战 211.6M
下载热血美职篮手游下载v1.19020.1722.0 安卓版
体育运动 1.62G
下载斗罗大陆魂师对决手游官方版下载v2.31.1 安卓版
卡牌对战 1.55G
下载坦克大决战游戏下载v1.9462 安卓官方正版
射击枪战 388.5M
下载孤胆车神维加斯官方正版(Gangstar Vegas)下载v8.5.1c 安卓手机版
射击枪战 2.44G
下载孤胆车神维加斯僵尸城版本下载v8.5.1c 安卓版
下载
英雄联盟云顶之弈手机版本下载v14.8.5768838 安卓最新版本
下载
苍蓝前线手游下载v1.1.0 安卓版
下载
崩坏学园2手游下载v12.2.8 安卓最新版本
下载
彩虹联萌手游下载v7.3.23015 安卓官方版
下载
花花填色苹果版下载v2.2.5 iPhone版
下载
奥特曼超时空英雄官方正版下载v1.0.2 安卓版
下载
全民奇迹2ios版下载v15.0.0 iphone版
下载