note 75307 added to function.array-unique
| From: | laurynas dot butkus at gmail dot com | Date: | Wed, 23 May 2007 11:22:43 +0000 |
| Subject: | note 75307 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126840@lists.php.net to get a copy of this message | ||
This one would work with objects and arrays also.
<?php
function my_array_unique($array, $keep_key_assoc = false)
{
$duplicate_keys = array();
$tmp = array();
foreach ($array as $key=>$val)
{
// convert objects to arrays, in_array() does not support objects
if (is_object($val))
$val = (array)$val;
if (!in_array($val, $tmp))
$tmp[] = $val;
else
$duplicate_keys[] = $key;
}
foreach ($duplicate_keys as $key)
unset($array[$key]);
return $keep_key_assoc ? $array : array_values($array);
}
?>
----
Server IP: 193.219.81.76
Probable Submitter: 195.14.188.109
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- https://master.php.net/note/edit/75307
Del: integrated -- https://master.php.net/note/delete/75307/integrated
Del: useless -- https://master.php.net/note/delete/75307/useless
Del: bad code -- https://master.php.net/note/delete/75307/bad+code
Del: spam -- https://master.php.net/note/delete/75307/spam
Del: non-english -- https://master.php.net/note/delete/75307/non-english
Del: in docs -- https://master.php.net/note/delete/75307/in+docs
Del: other reasons-- https://master.php.net/note/delete/75307
Reject -- https://master.php.net/note/reject/75307
Search -- https://master.php.net/manage/user-notes.php