Home > Backend Development > PHP Tutorial > Common ways to traverse php arrays

Common ways to traverse php arrays

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:13:49
Original
954 people have browsed it

1. One-dimensional array traversal

foreach($array as $value){
	echo $value;
}
Copy after login

2. Two-dimensional array traversal

foreach($array $key=>$val){
	echo $key.'=>'.$val;
}
Copy after login

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;
}
Copy after login
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;
}
Copy after login

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.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
How to create an array within an array?
From 1970-01-01 08:00:00
0
0
0
php array
From 1970-01-01 08:00:00
0
0
0
Array to array
From 1970-01-01 08:00:00
0
0
0
php array rotation
From 1970-01-01 08:00:00
0
0
0
array
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template