note 37364 added to function.array-search

From: Date: Wed, 12 Nov 2003 08:07:34 +0000
Subject: note 37364 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-60291@lists.php.net to get a copy of this message
I think array_search uses serial search because they binary search function i wrote here seems to do a better job for records that are not always at the beginning of the array. Here it is function binarySearch ( $a, $t, $l, $r ) { if($t<$a[$l]||$t>$a[$r])return NULL; while ( $l < $r ) { $m=intval($l+$r)/2; if($a[$m]==$t)return $m; elseif($t<$a[$m])$r=$m-1; elseif($t>$a[$m])$l = $m + 1; } if($t==$a[$r]) return $r; return NULL; } usage: binarySearch ( array, target, left range, right range ); if your array is a multidimensional array, simply change the comparison method. :) ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37364 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37364&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37364&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60291) next »