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)

3
  • count($array ['A']['A']['01']['02']); Commented Apr 1, 2016 at 8:44
  • Possible duplicate of Quick Way to Find the Largest Array in a Multidimensional Array? Commented Apr 1, 2016 at 8:48
  • It is, thanks for the info! I searched high and low, but didnt find what I was looking for, thank you Commented Apr 1, 2016 at 8:55

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, and it didnt slow down my script by much (executing this code around 4500 times added 2 seconds total page load time)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.