php中foreach()的用法
foreach()有两种用法:
1: foreach(array_name as $value)
{
statement;
}
{
statement;
}
这里的array_name是你要遍历的数组名,每次循环中,array_name数组的当前元素的值被赋给$value,并且数组内部的下标向下移一步,也就是下次循环回得到下一个元素。
2:foreach(array_name as $key => $value)
{
statement;
}
{
statement;
}
这里跟第一种方法的区别就是多了个$key,也就是除了把当前元素的值赋给$value外,当前元素的键值也会在每次循环中被赋给变量$key。键值可以是下标值,也可以是字符串。比如book[0]=1中的“0”,book[id]="001"中的“id”.
示范用法的更多例子:
1<?php
2/*-------------------------------------------------------------------------*/
3/* foreach example 1: value only */
4echo "foreach example 1: value only ".'<br />';
5
6$a = array(1, 2, 3, 17);
7
8foreach ($a as $v) {
9 echo "Current value of "$a: $v".'<br />';
10}
11
12/*-------------------------------------------------------------------------*/
13/* foreach example 2: value (with key printed for illustration) */
14echo '<br />'.'<br />'."foreach example 2: value (with key printed for illustration) ".'<br />';
15
16$a = array(1, 2, 3, 17);
17
18$i = 0; /* for illustrative purposes only */
19
20foreach ($a as $v) {
21 echo ""$a[$i] => $v".'<br />';
22 $i++;
23}
24
25/*-------------------------------------------------------------------------*/
26/* foreach example 3: key and value */
27echo '<br />'.'<br />'."foreach example 3: key and value ".'<br />';
28
29$a = array(
30 "one" => 1,
31 "two" => 2,
32 "three" => 3,
33 "seventeen" => 17
34);
35
36foreach ($a as $k => $v) {
37 echo ""$a[$k] => $v".'<br />';
38}
39
40/*-------------------------------------------------------------------------*/
41/* foreach example 4: multi-dimensional arrays */
42echo '<br />'.'<br />'."foreach example 4: multi-dimensional arrays ".'<br />';
43
44$a = array();
45$a[0][0] = "a";
46$a[0][1] = "b";
47$a[1][0] = "y";
48$a[1][1] = "z";
49
50foreach ($a as $v1) {
51 foreach ($v1 as $v2) {
52 echo "$v2"n";
53 }
54}
55
56/*-------------------------------------------------------------------------*/
57/* foreach example 5: dynamic arrays */
58echo '<br />'.'<br />'."foreach example 5: dynamic arrays ".'<br />';
59
60foreach (array(1, 2, 3, 4, 5) as $v) {
61 echo "$v"n";
62}
63?>
结果如下:
1foreach example 1: value only
2Current value of $a: 1
3Current value of $a: 2
4Current value of $a: 3
5Current value of $a: 17
6
7
8foreach example 2: value (with key printed for illustration)
9$a[0] => 1
10$a[1] => 2
11$a[2] => 3
12$a[3] => 17
13
14
15foreach example 3: key and value
16$a[one] => 1
17$a[two] => 2
18$a[three] => 3
19$a[seventeen] => 17
20
21
22foreach example 4: multi-dimensional arrays
23a b y z
24
25foreach example 5: dynamic arrays
261 2 3 4 5
Array
(
[0] => Array
(
[id] => 86
[name] => list_role
[title] => 角色列表
[pid] => 85
)
[1] => Array
(
[id] => 85
[name] => Index
[title] => 后台首页
[pid] => 83
)
[2] => Array
(
[id] => 83
[name] => Admin
[title] => 后台
[pid] => 0
)
)
foreach ($data as $v){}
Array
(
[id] => 86
[name] => list_role
[title] => 角色列表
[pid] => 85
)
Array
(
[id] => 85
[name] => Index
[title] => 后台首页
[pid] => 83
)
Array
(
[id] => 83
[name] => Admin
[title] => 后台
[pid] => 0
)
(
[id] => 86
[name] => list_role
[title] => 角色列表
[pid] => 85
)
Array
(
[id] => 85
[name] => Index
[title] => 后台首页
[pid] => 83
)
Array
(
[id] => 83
[name] => Admin
[title] => 后台
[pid] => 0
)