How to get the length of an Array in PHP

In this tutorial, we will show you to get the length of an array in PHP.

PHP how to get array length

There are many situations where you will want to know precisely how long your array is within PHP.

Luckily, getting an array’s length is a simple process thanks to one of PHP’s built-in functions.

Over the following few sections, you will see how you can use PHP’s count() function to get the length of an array.

If you haven’t yet been introduced to arrays, we recommend you check out our guide to arrays in PHP.

Getting the Length of an Array using PHP

The easiest way to get the length of an array within PHP is by utilizing the count() function. This PHP function will trawl through an array and count the number of elements within it, giving us the length.

Over the next few sections, we will show you how you can use the count() function in PHP to get the length of a standard array or a multi-dimensional one.

Getting the Length of a Flat Array

Let us start by exploring the simplest type of array to get the length of in PHP, a flat array. This is an array that doesn’t have any other arrays (dimensions) within it.

If you are dealing with a multi-dimensional array, this won’t behave as you might think. However, we will explore this behavior later on in this guide.

With a flat array in PHP, we can get the length of the array by simply passing it to the count() functions first parameter.

The value returned by the count() function will be the number of elements within the array, giving you its length.

count(ARRAYNAME);

Example of Getting the Length of a Flat Array in PHP

To showcase this behavior, let us create a simple array called “fruit” containing five elements, each being a different fruit name.

We will then pass this array into PHP’s count() function to get the length of the array. The result will be stored in the “length” variable.

Finally, we use the echo statement to print the array’s length to the screen.

<?php

$fruit = ['apple', 'banana', 'grape', 'orange', 'kiwi'];

$length = count($fruit);

echo $length;

?>

After running the above example, you should see that the PHP array’s length was printed on the screen.

5

Get the length of a Multi-dimensional Array in PHP

Getting the length of an array in PHP is a simple process thanks to the count() function. However, getting the length of a multi-dimensional array requires us to use an additional option.

By default, if you use the count() function on a multi-dimensional array, it will only get the length of that specific array. PHP will not count any embedded array’s elements.

However, by setting the second parameter of the count() function to “COUNT_RECURSIVE“, we can get the length of the entire multi-dimensional array.

count(ARRAYNAME, COUNT_RECURSIVE);

Get a Multi-dimensional Array Length without “COUNT_REVURSIVE

For the first example, let us show you what happens when you get the array length in PHP without “COUNT_RECURSIVE“.

We will create a simple multi-dimensional array with numbers 1 to 8. Three numbers will be stored within an array adding a dimension to our array. Technically there are 9 separate elements within this array (The embedded array is an element).

We pass our “example_array” into the count() function and store the returned length into the “array_length” variable.

The final result is displayed to you by utilizing the echo statement on the “array_length” variable.

<?php

$example_array = [
    1,
    2,
    3,
    [4,5,6],
    7,
    8
];

$array_length = count($example_array);

echo $array_length;

?>

After running the above code, you will see that you will get the following number. Immediately you should notice PHP returned the length of the array as being only 6 elements.

This is because the count() function, by default, does not recursively count an array, so PHP never counts the elements held within the embedded array.

6

Get Length of a Multi-Dimensional Array

To get the length of a multi-dimensional array in PHP you need to use the “COUNT_RECURSIVE” option with the count() function.

When “COUNT_RECURSIVE” has been set, the function will loop through the entirety of the array, counting each element.

Below you can see how the example looks with us setting the “COUNT_RECURSIVE” option with the count() function’s second parameter.

<?php

$example_array = [
    1,
    2,
    3,
    [4,5,6],
    7,
    8
];

$array_length = count($example_array, COUNT_RECURSIVE);

echo $array_length;

?>

The above code should produce the result that we have shown below. Compared to our previous result, you can see that it counted the three additional elements within the multi-dimensional array this time.

9

Why Avoid Using sizeof() to get an Array Size

While some tutorials might also recommend using sizeof(), it is simply an alias of count().

While sizeof() behaves the same as count(), you should avoid using this within your code. It would be best to avoid it because this function has a significantly different meaning in other programming languages, such as C.

In languages like C or C++, the sizeof() function is used to get the amount of memory allocated to a particular variable/object. For this reason, it is best to use the count() function to make your code more understandable.

Conclusion

At this point in the tutorial, you should now know how to get the length of an array within PHP.

Thanks to the count() function, getting an array’s size is a straightforward process. It handles all the grunt work of calculating how many elements exist within the given array.

Please comment below if you have any questions about getting the length of an array within the PHP language.

Be sure to check out our many other PHP tutorials to help master the language. We also have numerous other coding guides if you want to learn a new language.

Leave a Reply

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