Home  >  Article  >  Backend Development  >  php怎么遍历一个复杂的多维数组

php怎么遍历一个复杂的多维数组

WBOY
WBOYOriginal
2016-06-13 13:37:311228browse

php如何遍历一个复杂的多维数组
比如有这样一个数组,毫无规律,怎么遍历?

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$arr=array(
 array(array('val1','val2','val3'),'aa','bb'),
 'php',
'apache',
 array('hello','world')
);



------解决方案--------------------
你遍历后想做什么? 可以用递归处理
PHP code
// 作为回调函数。 参数将不会是数组
function doSomething($value) 
{
     echo $value;
}
function look(array $array)
{
     foreach($array as $value)
     {
         // 如果仍为数组,则继续遍历
         if(is_array($value))
             look($value);
         else
             call_user_func('doSomething', $value);
     }
} <div class="clear">
                 
              
              
        
            </div>
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