php之foreach遍历数组

foreach

(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)
    statement
foreach (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.

<?php
$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:

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

Warning

Reference 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:

<?php
$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:

<?php
$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:

<?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 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";
}
?>

Unpacking nested arrays with list()

(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:

<?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 example will output:

A: 1; B: 2
A: 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:

<?php
$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:

1
3

A notice will be generated if there aren't enough array elements to fill the list():

<?php
$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 7
A: 1; B: 2; C: 

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索array
, undefined reference
, foreach
, value
, to
, The
Example
php foreach遍历数组、foreach遍历数组、foreach遍历二维数组、c foreach遍历数组、js数组遍历foreach,以便于您获取更多的相关知识。

时间: 2024-10-28 23:17:51

php之foreach遍历数组的相关文章

php foreach 遍历数组的例子

foreach循环是一种变异的For循环,并允许您遍历数组中的元素.有两个不同版本的 foreach循环. foreach循环的语法如下: foreach (array as value) {    code to be executed; }     foreach (array as key => value) {    code to be executed; } 下面的例子演示了foreach循环,将打印给定数组的值: <?php $email = array('john.smith@

php foreach遍历数组用法介绍

语法 foreach ( array_expression as $key => $value ) statement 实例1:  代码如下 复制代码 <?php  $color=array('white' => '白色' ,        'black' => '黑色' ,        'red' => '红色' ,        'green' => '绿色',        'yellow' => '黄色');  foreach( $color as $c

php遍历数组 list foreach each方法总结

  在php中可以用来遍历数组的函数有很多,如有:for语句.list.each.foreach这四个函数,这也是在php中遍历数组的几个主要的函数,下面我来给大家介绍.   foreach遍历数组 我们在运用数组时,常常要遍历数组并获得各个键或者元素值,php提供了一些专门遍历数组的函数.这里先介绍foreach遍历数组函数的用法. 结构形式: foreach ( array_expression as $value ) statement /* array_expression是要遍历的数组

PHP foreach遍历多维数组实现方式_php技巧

介绍 正常我们的foreach可以按顺序把一维数组里面每个 key => value 打印出来,但是如果是多维数组则需要循环在嵌套循环,或则递归实现,但是这些方式都不够灵活,因为在不确定该数组是几维的情况下,不可能永无止境的嵌套循环,如果采用递归到可以解决,但是如果只想使用foreach全部循环出来该如何实现? 实现方式 一采用PHP本身自带的迭代器类 RecursiveIteratorIterator $test_arr = array(1,2,3,array(4,'aa'=>5,6,arr

PHP中使用foreach()遍历二维数组的简单实例_php技巧

第一种类型 想用foreach()遍历整个二维数组: $team = array('lk','ok'); $book = array('linux服务器配置与管理',$team); foreach($book as $k=>$val) //for $book each $value( as ) echo $k.'=>'.$val.''; 输出结果是: 0=>linux服务器配置与管理 1=>Array 当然,其实我是想要所有具体内容,而不是输出array... 所以应该采用如下做法

JavaScript使用forEach()与jQuery使用each遍历数组时return false 的区别_javascript技巧

 原生js使用forEach()与jquery使用each()遍历数组,return false 的区别: 1.使用each()遍历数组a,如下: var a=[20,21,22,23,24]; $.each(a, function(index,val) { console.log('index='+index); if(index==2){ return false; } console.log('val='+val); }); 结果如下: 从运行的效果可以看出,return 相当于循环中的b

Java中遍历数组使用foreach循环还是for循环?_java

从JDK1.5起,增加了新功能Foreach,它是for循环遍历数据的一种简写形式,使用的关键字依然是for,但参数格式不同.其详细用法为: for(Type e:collection){ //对变量e的使用} 参数说明: e:其类型Type是集合或数组中元素值的类型,该参数是集合或数组collection中的一个元素. collections: 要遍历的集合或数组,也可以是迭代器. 在循环体中使用参数e,该参数是foreach从集合或数组以及迭代器中取得的元素值,元素值是从头到尾进行遍历的.

php遍历数组的4种方法总结_php实例

在php中可以用来遍历数组的函数有很多,如有:for语句.list.each.foreach这四个函数,这也是在php中遍历数组的几个主要的函数,下面我来给大家介绍. foreach遍历数组 我们在运用数组时,常常要遍历数组并获得各个键或者元素值,php提供了一些专门遍历数组的函数.这里先介绍foreach遍历数组函数的用法. 结构形式: 复制代码 代码如下: foreach ( array_expression as $value ) statement /* array_expression

JavaScript forEach()遍历函数使用及介绍_javascript技巧

forEach()函数从头到尾把数组遍历一遍.有三个参数分别是:数组元素,元素的索引,数组本身(如果是一个参数就是数组元素,也就是数组的值. var data=[1,2,3,4,5,6]; var sum=0; data.forEach(function(v){//其中的v就是数组的值 123456 sum+=v;}) document.write(sum+"<br>");//打印出来是21 data.forEach(function(o,p,q){//分别对应:数组元素,