note 52822 deleted from function.in-array by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Jun 2009 21:45:47 +0000 |
| Subject: | note 52822 deleted from function.in-array by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156520@lists.php.net to get a copy of this message | ||
Note Submitter: greg at serberus dot co dot uk
----
in_array() doesn't seem to scale very well when the array you are searching becomes large. I
often need to use in_array() when building an array of distinct values. The code below seems to
scale better (even with the array_flip):
$distinct_words = array();
foreach ($article as $word) {
$flipped = array_flip($distinct_words);
if (!isset($flipped[$word]))
$distinct_words[] = $word;
}
This only works with arrays that have unique values.