php中的foreach函数
程序员文章站
2022-05-31 12:59:11
...
Foreach 函数(PHP4/PHP5)
foreach 语法结构提供了遍历数组的简单方式。
foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。
有两种语法:
[php]
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
例子1:
[php]
$arr=array(1,2,3,4);
foreach($arr as $value){
echo $value."
";
}
?>
$arr=array(1,2,3,4);
foreach($arr as $value){
echo $value."
";
}
?>
例子2:
[php]
$arr=array(1,2,3,4);
foreach($arr as $key=>$value){
echo $key."=====>".$value."
";
}
?>
$arr=array(1,2,3,4);
foreach($arr as $key=>$value){
echo $key."=====>".$value."
";
}
?>
上一篇: 详细解答JS操作剪贴板