Re: in_array algorithm
| From: | Lars Torben Wilson | Date: | Wed, 06 Feb 2002 22:45:24 +0000 |
| Subject: | Re: in_array algorithm | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-83773@lists.php.net to get a copy of this message | ||
On Wed, 2002-02-06 at 08:26, John Fulton wrote:
>
> Does anyone know which algorithm in_array() uses?
>
> For example, if I say
>
> in_array("foo", $arr)
>
> Does in_array() do an unordered sequential serach of $arr for
> "foo" which takes up to n comparisons [where n = count($arr)],
> or does it do a binary search which takes about lg(n) comparisons?
> Is it up to me to maintain a sorted array in the later case?
>
> Thanks,
> John
Well, the source for the currect version of that function (as of
4.2.0-dev) is here:
http://cvs.php.net/co.php/php4/ext/standard/array.c?r=1.156
Search down the page for 'php_search_array'--that's the function
which actually does the searching.
Looks like a simple sequential search to me.
Torben
--
Torben Wilson <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506