How to Find Size of Multidimensional Array in PHP

Last Updated on January 16, 2021 by Roshan Parihar

In this tutorial, learn how to find size of multidimensional array in PHP. The short answer is: use the PHP count() to get the accurate length of the multidimensional array.

You can also use the sizeof() function of PHP that gives the same result of the length of an array in the output. If you want to get the size of the inner array of multidimensional array in PHP, you can use the PHP foreach loop to parse through all the inner array elements and get the exact size in the output. You can find examples of using the method in this tutorial given here.

The multidimensional array is an array that contains another array as its elements. It means an array inside another array that can be called a sub-array of the array. Each array is located with a certain index. Let’s find out how to find the size of a multidimensional array and its inner array with the examples given below:

Find Size of Multidimensional Array Using PHP count()

To find the size of a Multidimensional array, you can use the PHP count() that takes an argument as the variable of the array. It checks for the number of elements inside an array and prints the size in the output as given below:

Output

3

The above example showing the size of the multidimensional array as 3 array elements inside it. We can also say that there are 3 sub-array inside an array.

Get Length with PHP sizeof()

Yet another method of getting the length of the array is PHP that works the same as the above function. The PHP sizeof() function takes an argument as the variable of the multidimensional array. It checks the length of the array that is the number of array elements inside the array as given below:

Output

3

The above example contains array elements inside the array. The output shows the number of array elements or the length of the multidimensional array is 3.

How to Get Inner Array Size of Multidimensional Array in PHP

In addition to the above all methods, you can also get the inner array elements size of a multidimensional array. You have to use the PHP foreach loop that traverses through all the array elements with the count() to get the inner array size as given below:

Output

3
4
5

The above output shows the multidimensional array contains 3 array elements out of which there are 3 elements in the 1st inner array, 4 elements in the 2nd array, 5 elements in the 3rd array.

All the methods are useful to get the length of the array. However, the last method traverses through all the elements and find the length of the inner array elements.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.