一、PHP foreach 循环语法
1.1 只获取数组的值
foreach ($array as $value) {
// code to be executed;
}
1.2 获取数组的下标和值
foreach ($array as $key => $value) {
code to be executed;
}
二、实例演示
2.1 索引数组
<?php
$colors = array("red","green","blue","yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
输出结果:
red
green
blue
yellow
2.1 关联数组
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
foreach($age as $x=>$x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
输出结果:
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43