1

I've got an array looking something like this:

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
                                )

I'm looking at the last level array, ie: ['A']['A']['01']['01']x and ['A']['A']['01']['02']x

At this level, in the example above, 01 has 2 items, 02 has 5 items, 03 could have 4 items, etc

I want to know, without iterating through all the items, what is the highest number of items, ie: in this example the highest number of items is 5 (not the value 05, but the number of items at this level of the array)

Mark
  • 290
  • 7
  • 23
  • `count($array ['A']['A']['01']['02']);` – Hanky Panky Apr 01 '16 at 08:44
  • Possible duplicate of [Quick Way to Find the Largest Array in a Multidimensional Array?](http://stackoverflow.com/questions/21861825/quick-way-to-find-the-largest-array-in-a-multidimensional-array) – Oldskool Apr 01 '16 at 08:48
  • It is, thanks for the info! I searched high and low, but didnt find what I was looking for, thank you – Mark Apr 01 '16 at 08:55

1 Answers1

1

Try

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

Test Script

[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;

?>

Output

[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
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • Perfect, and it didnt slow down my script by much (executing this code around 4500 times added 2 seconds total page load time) – Mark Apr 01 '16 at 09:01