"60","b"=>"56","c"=> array("a"=>"60","b"=>"56",array("a"=>"60","b"=>"56","c"=>ar" /> "60","b"=>"56","c"=> array("a"=>"60","b"=>"56",array("a"=>"60","b"=>"56","c"=>ar" />
PHP:7.4 首先,先创建一个数组; $array = array("a"=>"60","b"=>"56","c"=> array("a"=>"60","b"=>"56",array("a"=>"60","b"=>"56","c"=>ar
PHP:7.4
首先,先创建一个数组;
$array = array("a"=>"60","b"=>"56","c"=> array("a"=>"60","b"=>"56",array("a"=>"60","b"=>"56","c"=>array("a"=>"60","b"=>"56","c"=>"31"))) );
这个是个多维数组
这是主要函数
function go_array($array,$i = 0,$cut_line = "----|"){//$i当前维度 foreach ($array as $key => $value){//遍历当前数组 $cut = "";//初始化分割线 for ($o = 0; $o < $i; $o++) {//计算维度 $cut .= $cut_line;//当前维度分割线 } echo $cut."[".$key. "] => " . "[".$value."]
";//输出浏览器(输出终端 把
改成\n) if(is_array($value)){//判断$value是否维数组 $i = $i + 1;//加1为下一维数组 Go_array($value,$i);//执行一次此函数 $i = $i - 1;//减1为当前维数组 } }}
总代码如下:
"60","b"=>"56","c"=> array("a"=>"60","b"=>"56",array("a"=>"60","b"=>"56","c"=>array("a"=>"60","b"=>"56","c"=>"31"))) );go_array($array);function go_array($array,$i = 0,$cut_line = "----|"){ foreach ($array as $key => $value){ $cut = ""; for ($o = 0; $o < $i; $o++) { $cut .= $cut_line; } echo $cut."[".$key. "] => " . "[".$value."]
"; if(is_array($value)){ $i = $i + 1; go_array($value,$i); $i = $i - 1; } }}?>
浏览器输出结果 (如果要输出终端的话,请把
改成\n)
[a] => [60]
[b] => [56]
[c] => [Array]
----|[a] => [60]
----|[b] => [56]
----|[0] => [Array]
----|----|[a] => [60]
----|----|[b] => [56]
----|----|[c] => [Array]
----|----|----|[a] => [60]
----|----|----|[b] => [56]
----|----|----|[c] => [31]
希望能够帮到你
希望你能够点赞
希望你能够收藏
--结束END--
本文标题: PHP递归遍历输出任意长度多维数组算法(格式化打印数组算法)
本文链接: https://lsjlt.com/news/422072.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0