PHP中遍历二维数组_以不同形式的输出操作实例

时间:2022-12-02 10:32:33

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<body>
 
<?php
//定义二维索引数组
$arr = array(
 array("101","李军","男","1976-02-20","95033"),
 array("103","陆君","男","1974-06-03","95031"),
 array("105","匡明","男","1975-10-02","95031"),
 array("107","王丽","女","1976-01-23","95033"),
 array("108","曾华","男","1977-08-01","95033"),
 array("109","王芳","女","1975-02-10","95031")
);
第一种输出形式(echo语句)
/* echo "<table width=100% border='1'>";
echo "<tr><td>Sno</td><td>Sname</td><td>Ssex</td><td>Sbirthday</td><td>Class</td></tr>";
foreach($arr as $v){
 echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$v[2]}</td><td>{$v[3]}</td><td>{$v[4]}</td></tr>";
}
echo "</table>" ;
 
</body>
 
第二种输出形式(HTML代码 表格输出)
 
<body>
 
<table width=100% border='1'>
 <tr>
  <td>Sno</td>
  <td>Sname</td>
  <td>Ssex</td>
  <td>Sbirthday</td>
  <td>Class</td>
 </tr>
 
 
<?php
foreach($arr as $v){
  echo "<tr>
  <td>{$v[0]}</td>
  <td>{$v[1]}</td>
  <td>{$v[2]}</td>
  <td>{$v[3]}</td>
  <td>{$v[4]}</td>
  <tr>";
};
?>
</table>
 
</body>
 
第三种输出形式(下拉列表)
 
<body>
 
<?php
 
$arr = array(
 array("1","山东"),
 array("2","北京"),
 array("3","上海"),
 array("4","广州"),
 array("5","香港"),
 array("6","澳门")
);
?>
 
<select id="nation">
<option value="all">请选择</option>
 
 
<?php
foreach($arr as $v){
 echo "<option value='$v[0]'>$v[1]</option>";
};
?>
</select>
 
</body>
 
第四种输出形式
 
<body>
 
取数组中输出的总数
 
$arr = array(
 array(4)
);
echo $arr[0][0];
 
 
取数组中一组数据
$arr = array(
 array("1","上海")
);
echo "<option value='{$arr[0][0]}'>{$arr[0][1]}</option>";
 
 
取数组中任意一个数据
$arr = array(
 array("1","*"),
 array("2","北京"),
 array("3","上海"),
 array("4","广州"),
 array("5","香港")
);
echo "<option value='{$arr[4][0]}'>{$arr[4][1]}</option>";
?>
 
</body>

以上这篇PHP中遍历二维数组_以不同形式的输出操作实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/jly144000/archive/2017/08/23/7420604.html