PHP关联数组,获取最大项目数

时间:2016-04-01 08:39:27

标签: php arrays

我有一个看起来像这样的数组:

Array
(
    [A] => Array
        (
            [A] => Array
                (
                    [01] => Array
                        (
                            [01] => Array
                                (
                                    [0] => 01
                                    [1] => 02
                                )

                            [02] => Array
                                (
                                    [0] => 01
                                    [1] => 02
                                    [2] => 03
                                    [3] => 04
                                    [4] => 05
                                )

我正在查看最后一级的数组,即:[' A'] [' A'] [' 01'] [&# 39; 01'] x和[' A'] [' A'] [' 01'] [' 02'] x

在这个级别,在上面的例子中,01有2个项目,02有5个项目,03有4个项目等等

我想知道,在没有遍历所有项目的情况下,最多的项目数是多少,即:在此示例中,项目的最大数量是5(不是值05,而是此级别的项目数量)数组)

1 个答案:

答案 0 :(得分:1)

尝试

$max = max(array_map(function($_){return count($_);},$Array['A']['A']['01']));

测试脚本

[akshay@localhost tmp]$ cat test.php
<?php

$Array = array("A"=>array("A"=>array(
    "01"=>array(
        "01"=>array('01','02'),
        "02"=>array('01','02','03','04','05')
    )
)));

// Input
print_r($Array);

$max = max(array_map(function($_){return count($_);},$Array['A']['A']['01']));

// Output
echo $max.PHP_EOL;

?>

<强> 输出

[akshay@localhost tmp]$ php test.php
Array
(
    [A] => Array
        (
            [A] => Array
                (
                    [01] => Array
                        (
                            [01] => Array
                                (
                                    [0] => 01
                                    [1] => 02
                                )

                            [02] => Array
                                (
                                    [0] => 01
                                    [1] => 02
                                    [2] => 03
                                    [3] => 04
                                    [4] => 05
                                )

                        )

                )

        )

)
5