I'm not sure why PHP doesn't provide a way to specify a binary search. Here's an example of the performance gains, for this array size, about 50x improvement using interpreted PHP. If built in, it could probably achieve around 1000x improvement, again for this array size.
<?php
$X = array(1);
for ($j = 1; $j < 50000; ++$j)
$X[] = $X[$j - 1] + rand(1, 6);
$x = -microtime(true);
$m = 0;
for ($j = 0; $j < 10000; ++$j)
$m += in_array(rand(1, 175000), $X);
$x += microtime(true);
echo $x.PHP_EOL;
$x = -microtime(true);
$m = 0;
for ($j = 0; $j < 10000; ++$j)
$m += binarySearch($X, rand(1, 175000));
$x += microtime(true);
echo $x.PHP_EOL;
function binarySearch($array, $value) {
$low = 0;
$high = count($array) - 1;
while ($low <= $high) {
$pivot = floor(($low + $high) / 2);
if ($array[$pivot] == $value)
return true;
if ($value < $array[$pivot])
$high = $pivot - 1;
else
$low = $pivot + 1;
}
return false;
}