[PHP 4, PHP 5, PHP 7, PHP 8]
count — Counts all elements in an array or in a Countable object
Description
count[Countable|array $value
, int $mode
= COUNT_NORMAL
]: int
Parameters
value
An array or Countable object.
mode
If
the optional mode
parameter is set to COUNT_RECURSIVE
[or 1], count[] will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.
Caution
count[] can detect recursion to avoid an infinite loop, but will emit an E_WARNING
every time it does [in case the array contains itself more than once] and return a count
higher than may be expected.
Return Values
Returns the number of elements in value
. Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, 1
would be returned, unless value
was null
, in which case
0
would be returned.
Changelog
8.0.0 | count[] will now throw TypeError on invalid countable types passed to the value parameter.
|
7.2.0 | count[] will now yield a warning on invalid countable types passed to the value parameter.
|
Examples
Example #1 count[] example
The above example will output:
Example #2 count[] non Countable|array example [bad example - don't do this]
The above example will output:
Output of the above example in PHP 7.2:
int[3] Warning: count[]: Parameter must be an array or an object that implements Countable in … on line 12 int[0] Warning: count[]: Parameter must be an array or an object that implements Countable in … on line 14 int[1]
Output of the above example in PHP 8:
int[3] Fatal error: Uncaught TypeError: count[]: Argument #1 [$var] must be of type Countable .. on line 12
Example #3 Recursive count[] example
The above example will output:
Example #4 Countable object
The above example will output:
See Also
- is_array[] - Finds whether a variable is an array
- isset[] - Determine if a variable is declared and is different than null
- empty[] - Determine whether a variable is empty
- strlen[] - Get string length
- is_countable[] - Verify that the contents of a variable is a countable value
- Arrays
onlyranga at gmail dot com ¶
8 years ago
[Editor's note: array at from dot pl had pointed out that count[] is a cheap operation; however, there's still the function call overhead.]
If you want to run through large arrays don't use count[] function in the loops , its a over head in performance, copy the count[] value into a variable and use that value in loops for a better performance.
Eg:
// Bad approach
for[$i=0;$i
Anonymous ¶
2 years ago
For a Non Countable Objects
$count = count[$data];
print "Count: $count\n";
Warning: count[]: Parameter must be an array or an object that implements Countable in example.php on line 159
#Quick fix is to just cast the non-countable object as an array..
$count = count[[array] $data];
print "Count: $count\n";
Count: 250
lucasfsmartins at gmail dot com ¶
3 years ago
If you are on PHP 7.2+, you need to be aware of "Changelog" and use something like this:
You can organize your code to ensure that the variable is an array, or you can extend the Countable so that you don't have to do this check.
danny at dannymendel dot com ¶
15 years ago
I actually find the following function more useful when it comes to multidimension arrays when you do not want all levels of the array tree.
// $limit is set to the number of recursions
alexandr at vladykin dot pp dot ru ¶
15 years ago
My function returns the number of elements in array for multidimensional arrays subject to depth of array. [Almost COUNT_RECURSIVE, but you can point on which depth you want to plunge].
pied-pierre ¶
7 years ago
A function of one line to find the number of elements that are not arrays, recursively :
function count_elt[$array, &$count=0]{
foreach[$array as $v] if[is_array[$v]] count_elt[$v,$count]; else ++$count;
return $count;
}
php_count at cubmd dot com ¶
5 years ago
All the previous recursive count solutions with $depth option would not avoid infinite loops in case the array contains itself more than once.
Here's a working solution: