Home >Backend Development >PHP Tutorial >Common ways to traverse php arrays
1. One-dimensional array traversal
foreach($array as $value){ echo $value; }
2. Two-dimensional array traversal
foreach($array $key=>$val){ echo $key.'=>'.$val; }
3. Multi-dimensional array traversal
public static function multi_arr_foreach($arr) { static $data; if (!is_array ($arr)) { return $data; } foreach ($arr as $key => $val ) { if (is_array ($val)) { self::multi_arr_foreach($val); } else { $data[]=$val; } } return $data; }But I saw a very short way of writing it, and I don’t quite understand it yet
function loop_array($arr){ $value = is_array($arr) ? array_map('loop_array',$arr) : $arr; return $value; }
The above introduces the common methods of PHP array traversal, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.