note 96198 modified in function.in-array by danbrown
| From: | danbrown@php.net | Date: | Sun, 14 Feb 2010 04:56:29 +0000 |
| Subject: | note 96198 modified in function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165944@lists.php.net to get a copy of this message | ||
If you're working with very large 2 dimensional arrays (eg 20,000+ elements) it's much
faster to do this...
<?php
$needle = 'test for this';
$flipped_haystack = array_flip($haystack);
if ( isset($flipped_haystack[$needle]) )
{
print "Yes it's there!";
}
?>
I had a script that went from 30+ seconds down to 2 seconds (when hunting through a 50,000 element
array 50,000 times).
Remember to only flip it once at the beginning of your code though!
--was--
If you're working with very large 2 dimensional arrays (eg 20,000+ elements) it's much
faster to do this...
$needle = 'test for this';
$flipped_haystack = array_flip($haystack);
if ( isset($flipped_haystack[$needle]) )
{
print "Yes it's there!";
}
I had a script that went from 30+ seconds down to 2 seconds (when hunting through a 50,000 element
array 50,000 times).
Remember to only flip it once at the beginning of your code though!
http://php.net/manual/en/function.in-array.php