php计算几层数组

php计算多层数组

函数

function countarray($arr){
    $count = 1;
    $max = 0;
    foreach ($arr as $key => $value) {
        if(is_array($value)){
            $ncount = count array($value);
            if($ncount > $max){
                $max = $ncount;
            }
        }
    }
    $count += $max;
    return $count;
}

测试

$arr = array(
    "web" => array(
        "0" => array(
                "value" => array(
                    "0" => "您好",
                    "1" => "你好",
                    "2" => array(
                        "0" => "您好",
                        "1" => "你好",
                        "2" => array(
                            "0" => "您好",
                            "1" => "你好",
                            "2" => "hello"
                        )
                    )
                ),
                "key" => "Hello"
            ),
        "1" => array(
                "value" => array(
                    "0" => "hi",
                    "1" => "hh",
                    "2" => "hello"
                    ),
                "key" => "Hello Kitty"
            ),
        "2" => array(
                "value" => array(
                    "0" => "hello",
                    "1" => "test",
                    ),
                "key" => "Hello"
            ),
        )
);
echo countarray($arr); // 6
none

发表评论

0条评论

设置
配色方案

布局

现在购买