介绍

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 DisplayArray($array, $i = 0, $cutLine = "----|")
{ //$i当前维度
    foreach ($array as $key => $value) { //遍历当前数组
        $cut = ""; //初始化分割线
        for ($o = 0; $o < $i; $o++) { //计算维度
            $cut .= $cutLine; //当前维度分割线
        }
        if (is_array($value)) { //判断$value是否维数组
            echo $cut . "[" . $key . "] => " . "[Array]<br>\r\n"; //输出结果
            $i = $i + 1; //加1为下一维数组
            DisplayArray($value, $i); //执行一次此函数
            $i = $i - 1; //减1为当前维数组
        } else {
            echo $cut . "[" . $key . "] => " . "[" . $value . "]<br>\r\n"; //输出结果
        }
    }
}

示例

<?php
$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"))) );
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;
        }
        if(is_array($value)){
            echo $cut."[".$key. "] => " . "[Array]<br>";
            $i = $i + 1;
            go_array($value,$i);
            $i = $i - 1;
        }else{
            echo $cut."[".$key. "] => " . "[".$value."]<br>";
        }
    }
}
?>

[a] => [60]
[b] => [56]
[c] => [Array]
----|[a] => [60]
----|[b] => [56]
----|[0] => [Array]
----|----|[a] => [60]
----|----|[b] => [56]
----|----|[c] => [Array]
----|----|----|[a] => [60]
----|----|----|[b] => [56]
----|----|----|[c] => [31]

结果

在此,分享就结束了

希望我写的代码能帮助到大家,别忘记留言哦~