1.each
each — 返回数组中当前的键/值对并将数组指针向前移动一步
$arr = array("one", "two", "three"); reset($arr); ##记得重置数据指针 while (list($key, $value) = each($arr)) { echo "Key: $key; Value: $value<br /> "; } # 两者代码功能相同 foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br /> "; }
2.list
list — 把数组中的值赋给一组变量 array list ( mixed $var1
[, mixed $...
] ) ,与array一样,同是语言结构.
$result = $pdo->query("SELECT id, name, salary FROM employees"); while (list($id, $name, $salary) = $result->fetch(PDO::FETCH_NUM)) { echo " <tr> " . " <td><a href="info.php?id=$id">$name</a></td> " . " <td>$salary</td> " . " </tr> "; }