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

Common ways to traverse php arrays

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:13:49955browse

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.

Statement:
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
Previous article:PHP socket 概述Next article:echo使用!