note 93483 added to function.array-search
| From: | blee at osu1 dot php dot net | Date: | Fri, 11 Sep 2009 22:21:52 +0000 |
| Subject: | note 93483 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160481@lists.php.net to get a copy of this message | ||
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++;
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 24.173.33.58
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/93483
Del: integrated -- https://master.php.net/note/delete/93483/integrated
Del: useless -- https://master.php.net/note/delete/93483/useless
Del: bad code -- https://master.php.net/note/delete/93483/bad+code
Del: spam -- https://master.php.net/note/delete/93483/spam
Del: non-english -- https://master.php.net/note/delete/93483/non-english
Del: in docs -- https://master.php.net/note/delete/93483/in+docs
Del: other reasons-- https://master.php.net/note/delete/93483
Reject -- https://master.php.net/note/reject/93483
Search -- https://master.php.net/manage/user-notes.php