This article introduces an example of recursively printing all elements in an array in PHP. Friends in need can refer to it.
In PHP programming, use recursive algorithm to print out all elements in the array. The custom function is as follows: <?php /** * 递归打印数组中所有元素 * edit bbs.it-home.org */ //@patten:为打印前的字符串,不同的层次会增加该串打印的次数 //@array:为要打印的数组 function print_array($patten, $array){ $retstr; foreach($array as $value){ if( is_array($value) ){ $patten = $patten.$patten; print_array($patten, $value); } else { echo "<p>".$patten."[".key($array)."]".": ".$value." <br/> ". "</p>"; } next($array); } } ?> Copy after login Calling method: print_array("-", $array); Copy after login |