1.2.1348. Use Recursive count()¶
The native count() function is recursive: it can count all the elements inside multi-dimensional arrays.
The second argument of count, when set to COUNT_RECURSIVE
, count recursively the elements.
Recursive count() counts all the elements, includeing the recusrive elements themselves. For a 2 dimensional array, this means removing the normal count of elements from the list. For higher dimensions, removing the recursive elememnts requires better filtering.
<?php
$array = array( array(1,2,3), array(4,5,6));
print (count($array, COUNT_RECURSIVE) - count($array, COUNT_NORMAL));
$count = 0;
foreach($array as $a) {
$count += count($a);
}
print $count;
?>
See also count.
1.2.1348.1. Suggestions¶
Drop the loop and use the 2nd argument of count()
1.2.1348.2. Specs¶
Short name |
Structures/UseCountRecursive |
Rulesets |
|
Exakat since |
1.1.7 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
Very high |
Examples |
|
Available in |