PHP foreach 关键字
示例
打印数组中的所有值
<?php
$cars = ["Ford", "Volvo", "BMW"];
foreach($cars as $car) {
echo "$car <br>";
}
?>
自己试试 »
定义和用法
The foreach
keyword is used to create foreach loops, which loops through a block of code for each element in an array.
相关页面
在我们的 PHP foreach 循环教程 中了解更多关于 foreach 循环的信息。
更多示例
示例
打印关联数组的键值
<?php
$people = [
"Peter" => "35",
"Ben" => "37",
"Joe" => "43"
];
foreach($people as $person => $age) {
echo "$person is $age years old";
echo "<br>";
}
?>
自己试试 »
❮ PHP 关键字