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.
Learn more about PHP arrays.
PHP array length content
PHP count() function
This function returns the array length in PHP or a countable object. This function counts the number of elements in the array or object. This method is the best and most common way to get the count of array items.
count(array, mode);
If you use this function with an array parameter, it counts all elements in the array. When use with an object that implements the Countable interface, it returns the return value of the method Countable::count().
array | Required | An array or countable object. |
mode | Optional | If 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 Value | Returns 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
$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 in an array. In this section of the PHP, the size of the array and the count of the array are the same.
sizeof(array, mode);
array | Required | Specify an array |
mode | Optional | 0: 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 Value | Returns 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 elements of an array. Learn more about PHP loops in our tutorial.
$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 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