note 43114 added to function.array-unique
| From: | csaba at alum dot mit dot edu | Date: | Wed, 09 Jun 2004 23:17:00 +0000 |
| Subject: | note 43114 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-70714@lists.php.net to get a copy of this message | ||
The following is an efficient, adaptable implementation of array_unique which always retains the
first key having a given value:
function array_unique2(&$aray) {
$aHash = array();
foreach ($aray as $key => &$val) if (@$aHash[$val]++) unset ($aray[$key]);
}
It is also adaptable to multi dimensional arrays. For example, if your array is a sequence of
(multidimensional) points, then in place of @$aHash[$val]++ you could use
@$aHash[implode("X",$val)]++
If you want to not have holes in your array, you can do an array_merge($aray) at the end.
Csaba Gabor
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43114
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43114&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43114&report=yes
Search -- http://master.php.net/manage/user-notes.php