note 114489 deleted from function.count by cmb
| From: | cmb@php.net | Date: | Tue, 04 Oct 2016 18:05:05 +0000 |
| Subject: | note 114489 deleted from function.count by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206765@lists.php.net to get a copy of this message | ||
Note Submitter: norbyte
----
If you want to check whether large arrays contain a specific (less) number of array elements,
don't use count(). This is very slow because each element has to get iterated.
This is much faster:
function isArrayCount(array $array, $count) {
$count = (int) $count;
$result = reset($array);
if ($count === 0) {
// for empty arrays reset returns FALSE
// we have to switch the boolean result
$result = !$result;
}
for($i = 1; $i < $count; $i++) {
if (next($array) === FALSE) {
$result = FALSE;
break;
}
}
return $result;
}