I use a lot of in_array functions and it seems to bog down my loading times. I found the following code in the in_array php documentation. The writer states "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."
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;
}
However the function works, but only half of the time, sometimes it doesnt output everything it should be outputting for example if I have an array with apples, oranges, and lemons, and do an match for apples and oranges it will only print oranges or something weird. Could someone please explain to me what exactly this script does, and why it doesn't work as a substitute for in_array.
array_fill_keys($yourarray,1)). keep in mind that this will only work if the elements are unique though. edit: values have to be scalar too.