How to Find the Size/length of a Multidimensional Array in PHP?

Problem:

You have a multidimensional array. You want to calculate how many elements are inside that array.

 

Solution:

You can count number of elements using following two ways. Please note that each method shows different element number. Choose which one is fit for you.

 

Method 1: flattening the array at first and then count element number

There are 2 steps in this method.

  • Flatten the multidimensional array to one-dimensional array, then
  • Use the count() function to calculate the element numbers.

See the example below-

<?php
$month = array("December",
              array("January"),
              array("February"),
                array("March", "April", array("May", "June")),
              array("July"),
              array("August", "September", "October"),
              array("November", "December")
    );

$recur_flat_arr_obj =  new RecursiveIteratorIterator(new RecursiveArrayIterator($month));
$flat_arr = iterator_to_array($recur_flat_arr_obj, false);
echo "Total elements are ". count($flat_arr);
?>

[wpdm_file id=79]

Output:
Total elements are 13

Line 10-11 Flatten the multidimensional array to one dimensional array. If you print the $flat_arr[] array, you’ll see the array.
Line 12 count() function calculates the number of elements of the $flat_arr[] array.

If you can’t understand how the array flatting works above, see the following tutorial for detail explanation-

How to Flatten/Convert a Multidimensional Array into a Simple One in PHP?

 

Method 2: Using COUNT_RECURSIVE as second parameter in the count() function

The count() function can take 2 parameters. First one is an array and the second one is COUNT_NORMAL which counts all the elements of the nested array, if any, existed inside the main array. When using the both parameters, the count() function calculates the number of elements of the array (including any nested array inside) that is set in the first parameter. See the following example-

<?php
$month = array("December",
                array("January"),
                array("February"),
                array("March", "April", array("May", "June")),
                array("July"),
                array("August", "September", "October"),
                array("November", "December")
        );
echo "Total elements are ". count($month, COUNT_RECURSIVE);
?>

[wpdm_file id=80]

Output:
Total elements are 20

How it works:
As you see the total number is 20, not 13, as because each nested array occupies an index number (see the following example). There are 7 nested arrays inside the main array. So, it sums it to 20 (13+7=20).

<?php
    $month = array(0=>"December",
                   1=>array("January"),
                   2=>array("February"),
                   3=>array("March", "April", array("May", "June")),
                   4=>array("July"),
                   5=>array("August", "September", "October"),
                   6=>array("November", "December")
             );
?>