php语法中有哪些遍历数组方法
php语法中有哪些遍历数组方法,下面编程教程网小编给大家介绍php遍历的五种方法!
利用array_map()函数遍历
$arr = ["香蕉","苹果","梨子","橙子","橘子","榴莲"];
var_dump($arr);
$arr = array_map(function ($item) {
return $item . '_i';
}, $arr);
var_dump($arr);
利用array_walk()函数遍历
$arr = ["香蕉","苹果","梨子","橙子","橘子","榴莲"];
array_walk($arr, function (&$item) {
$item = $item . '_i';
});
var_dump($arr);
利用for循环遍历
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
for ($i=0; $i < count($array); $i++) {
echo $array[$i] . "<br/>";
}
利用foreach循环遍历
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
foreach ($array as $val){
echo "值是:" . $val ;
echo "<br/>";
}
echo "<br/>";
foreach ($array as $key => $value) {
echo "键名为:".$key.",键值为:".$value . "<br/>";
}
利用while循环+each()+list()遍历
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
while( list($key, $val) = each($array) ) {
echo "$key => $val<br>";
}
以上是编程学习网小编为您介绍的“php语法中有哪些遍历数组方法”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。