Return the total count of elements in an array.
<?php $cars=array(“Volvo”,“BMW”,“Toyota”); echo count($cars); ?> |
The count() function returns the total number of elements in an array.
count(array, mode) |
Parameter |
Description |
array |
Required. Specifies the array. |
mode |
Optional. Specifies the mode. Possible values are: · 0 – Default. Does not count elements in multidimensional arrays. · 1 – Counts the array recursively (includes all elements in multidimensional arrays). |
Return Value: |
Returns the number of elements in the array |
PHP Version: |
4+ |
PHP Changelog: |
The mode parameter was added in PHP 4.2 |
Count all elements in the array, including those in nested arrays.
<?php $cars=array ( “Volvo”=>array ( “XC60”, “XC90” ), “BMW”=>array ( “X3”, “X5” ), “Toyota”=>array ( “Highlander” ) ); echo “Normal count: “ . count($cars).“<br>”; echo “Recursive count: “ . count($cars,1); ?> |