21,893
社区成员




<?php
function Array_BinarySearch( $needle, $haystack, $comparator , &$probe )
{
$high = Count( $haystack ) -1;
$low = 0;
while ( $high >= $low )
{
$probe = Floor( ( $high + $low ) / 2 );
$comparison = $comparator( $haystack[$probe], $needle );
if ( $comparison < 0 )
{
$low = $probe +1;
}
elseif ( $comparison > 0 )
{
$high = $probe -1;
}
else
{
return true;
}
}
//The loop ended without a match
//Compensate for needle greater than highest haystack element
if($comparator($haystack[count($haystack)-1], $needle) < 0)
{
$probe = count($haystack);
}
return false;
}
?>
参考:http://php.net/manual/en/function.array-search.php 下面的评论