打印数组中的所有值:
<?php
$cars = ["Ford", "Volvo", "BMW"];
foreach($cars as $car) {
echo "$car <br>";
}
?>
亲自试一试 »
这个foreach
关键字用于创建 foreach 循环,该循环遍历数组中每个元素的代码块。
阅读有关 foreach 循环的更多信息PHP foreach 循环教程。
打印关联数组中的键和值:
<?php
$people = [
"Peter" => "35",
"Ben" => "37",
"Joe" => "43"
];
foreach($people as $person => $age) {
echo "$person is $age years old";
echo "<br>";
}
?>
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!