note 52822 added to function.in-array
| From: | greg at serberus dot co dot uk | Date: | Fri, 13 May 2005 14:50:22 +0000 |
| Subject: | note 52822 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89381@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52822
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52822&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52822&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52822&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52822&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52822&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52822&report=yes
Search -- http://master.php.net/manage/user-notes.php