note 16802 modified in function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Nov 2008 15:11:41 +0000 |
| Subject: | note 16802 modified in function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146239@lists.php.net to get a copy of this message | ||
I needed a way of retaining the original array's keys in the new, unique array. I came up with
this. It works for my purposes but may need refinement.
<?php
function my_array_unique($somearray)
{
asort($somearray);
reset($somearray);
$currentarrayvar = current($somearray);
foreach ($somearray as $key=>$var)
{
if (next($somearray) != $currentarrayvar)
{
$uniquearray[$key] = $currentarrayvar;
$currentarrayvar = current($somearray);
}
}
reset($uniquearray);
return $uniquearray;
}
?>
--was--
I needed a way of retaining the original array's keys in the new, unique array. I came up with
this. It works for my purposes but may need refinement.
function my_array_unique($somearray)
{
asort($somearray);
reset($somearray);
$currentarrayvar = current($somearray);
foreach ($somearray as $key=>$var)
{
if (next($somearray) != $currentarrayvar)
{
$uniquearray[$key] = $currentarrayvar;
$currentarrayvar = current($somearray);
}
}
reset($uniquearray);
return $uniquearray;
}
http://php.net/manual/en/function.array-unique.php