Return the number of elements in an array:
<?php$cars=array("Volvo","BMW","Toyota");echo count($cars);?>The count() function returns the number of elements in an array.
count( array,mode );
parameter | describe |
---|---|
array | Required. Specifies the array to count. |
mode | Optional. Specifies the mode of the function. Possible values: 0 - Default. Not counting all elements in a multidimensional array. 1 - Count the number of elements in an array recursively (count all elements in a multidimensional array). |
Return value: | Returns the number of elements in the array. |
---|---|
PHP version: | 4+ |
Update log: | The mode parameter is new in PHP 4.2. |
Recursively count the number of elements in an array:
<?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);?>