php array length size count

PHP Array Length (4 Methods)

On: Sep 25, 2022 / By: Honar Systems / Categories: Used before category names. PHP

In PHP, arrays are versatile data structures that allow you to store multiple values in a single variable. One common task when working with a PHP array is determining its length or the number of elements it contains. PHP array length, count, size, and others are expressions to find how many items are in the array. In this tutorial, we are going to cover all of them and the differences between them.

PHP array length content

Using PHP count() function

The count() function is a built-in PHP function that returns the number of elements in an array or a countable object. It works with both indexed and associative arrays. This function counts the number of elements in the array or object. This method is the best and most common way to get the length of array items in PHP.

count(array, mode);

If you use this function with an array parameter, it counts all elements in the array. When used with an object that implements the Countable interface, it returns the return value of the method Countable::count().

arrayRequiredAn array or countable object.
modeOptionalIf you use the mode with “1” (COUNT_RECURSIVE), this function will recursively count the array’s items. This is particularly useful for counting all the elements of a multidimensional array.
0 – Default. Does not count all elements of multidimensional arrays. Only returns the count of the first level of the array.
1 – Counts the array recursively (counts all the elements of multidimensional arrays)
Returned ValueReturns the number of elements in the array.
$cars = array("fer" => "Ferrari", "ben" => "Benz", "bmw" => "BMW", "vol" => "Volvo");

var_dump(count($cars));

Output

4

Multidimensional array length

A multidimensional array is an array that contains other arrays as its elements. In other words, it is an array of arrays.

In PHP, you can determine the length or size of a multidimensional array using the techniques below.

$cars = array(
    "Volvo" => array(
        "XC60",
        "XC90"
    ),
    "BMW" => array(
        "X3",
        "X5"
    ),
    "Toyota" => array(
        "Highlander"
    )
);

var_dump(count($cars)); //Normal
var_dump(count($cars, 1)); //Recursive 

Output

3

8

sizeof() function

PHP sizeof() function is a built-in function that returns the number of elements (length) in an array and is an alias of the count() function. In this section of PHP, the size of the array and the count of the array are the same.

sizeof(array, mode);
arrayRequiredSpecify an array
modeOptional0: It is the default, does not count all elements of multidimensional arrays
1: It counts the array recursively (It counts all the elements with multidimensional arrays)
Returned ValueReturns the number of elements in the array.
$cars = array("fer" => "Ferrari", "ben" => "Benz", "bmw" => "BMW", "vol" => "Volvo");

var_dump(sizeof($cars));

Output

4

This function is the same as the count() function.

$cars = array(
    "Volvo" => array(
        "XC60",
        "XC90"
    ),
    "BMW" => array(
        "X3",
        "X5"
    ),
    "Toyota" => array(
        "Highlander"
    )
);

var_dump(sizeof($cars)); //Normal
var_dump(sizeof($cars, 1)); //Recursive 

Output

3

8

Array length with PHP loops

With loops in PHP, you can count the elements of an array. Learn more about PHP loops in our tutorial.

Another approach to obtaining the length of an array is by using a foreach loop. This method iterates over each element of the array and keeps count using a variable.

$cars = array("fer" => "Ferrari", "ben" => "Benz", "bmw" => "BMW", array("vol" => "Volvo", "toy" => "Toyota"));

$count = 0;
foreach ($cars as $car) {
    $count++;
}

var_dump($count);

Output

4

With the following code, you can count multidimensional arrays with nested PHP loops.

function array_count($array)
{
    static $counter = 0;
    if (is_array($array)) {
        foreach ($array as $item) {
            if (is_array($item)) {
                array_count($item);
            } else {
                $counter++;
            }
        }
    }
    return $counter;
}

$cars = array("fer" => "Ferrari", "ben" => "Benz", "bmw" => "BMW", array("vol" => "Volvo", "toy" => "Toyota"));

var_dump(array_count($cars));

Output

5

Using the array_key_last() Function

If you’re working with an associative array and want to determine its length, you can use the array_key_last() function in conjunction with array_keys() to get the last key and calculate the length.

$cars = array("fer" => "Ferrari", "ben" => "Benz", "bmw" => "BMW", "vol" => "Volvo");

$keys = array_keys($cars);
$lastKey = array_key_last($keys);
$length = $lastKey + 1;

var_dump($length);

Output

4

This method is not a popular method because the count() function can do the same to find an array length in PHP.