note 92460 deleted from function.in-array by cmb
| From: | cmb@php.net | Date: | Sun, 15 Sep 2019 13:03:26 +0000 |
| Subject: | note 92460 deleted from function.in-array by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212190@lists.php.net to get a copy of this message | ||
Note Submitter: robin at robinnixon dot com
----
This function is five times faster than in_array(). It uses a binary search and should be able to be
used as a direct replacement:
<?php
function fast_in_array($elem, $array)
{
$top = sizeof($array) -1;
$bot = 0;
while($top >= $bot)
{
$p = floor(($top + $bot) / 2);
if ($array[$p] < $elem) $bot = $p + 1;
elseif ($array[$p] > $elem) $top = $p - 1;
else return TRUE;
}
return FALSE;
}
?>