note 114489 added to function.count
| From: | norbyte | Date: | Wed, 26 Feb 2014 12:06:34 +0000 |
| Subject: | note 114489 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198731@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.count.php
Edit -- https://master.php.net/note/edit/114489
Del: integrated -- https://master.php.net/note/delete/114489/integrated
Del: useless -- https://master.php.net/note/delete/114489/useless
Del: bad code -- https://master.php.net/note/delete/114489/bad+code
Del: spam -- https://master.php.net/note/delete/114489/spam
Del: non-english -- https://master.php.net/note/delete/114489/non-english
Del: in docs -- https://master.php.net/note/delete/114489/in+docs
Del: other reasons-- https://master.php.net/note/delete/114489
Reject -- https://master.php.net/note/reject/114489
Search -- https://master.php.net/manage/user-notes.php