note 93483 deleted from function.array-search by danbrown
| From: | danbrown@php.net | Date: | Fri, 11 Sep 2009 22:27:32 +0000 |
| Subject: | note 93483 deleted from function.array-search by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160482@lists.php.net to get a copy of this message | ||
Note Submitter: blee
----
The function provided by temporal dot pl at gmail dot com below is incomplete. Because the probe is
calculated using the floor() rounding function the loop will iterate an extra time in the following
example:
<?php
$testArr = array(10, 30, 40, 50);
$res = Array_BinarySearch(20, $testArr, 'CompareNumbers', $probe);
echo (int)$res.' '.$probe.'<br />';
//output should be: 0 1
//actual output is: 0 0
?>
A possible solution is to add an additional compensator before returning false:
<?php
if($comparator( $haystack[$probe], $needle ) < 0){
$probe++;
}
?>