note 58237 modified in function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Nov 2008 15:07:09 +0000 |
| Subject: | note 58237 modified in function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146228@lists.php.net to get a copy of this message | ||
array_unique function starts its comparation from beginning and pop the key off if there is more
values inside array. The last one remains. But i needed to hold priority of the order of values and
let the first one in.
Here is my easy solution:
<?php
function my_array_unique($from)
{
for ($i=count($from);$i>1;$i--)
{
$last = $from[$i];
$from[$i] = "";
if (!in_array($last,$from))
$from[$i]=$last;
}
return array_unique($from);
}
# One empty value remains in array.
# But its very easy to separate it while using output array.
?>
--was--
array_unique function starts its comparation from beginning and pop the key off if there is more
values inside array. The last one remains. But i needed to hold priority of the order of values and
let the first one in.
Here is my easy solution:
##---
function my_array_unique($from)
{
for ($i=count($from);$i>1;$i--)
{
$last = $from[$i];
$from[$i] = "";
if (!in_array($last,$from))
$from[$i]=$last;
}
return array_unique($from);
}
# One empty value remains in array.
# But its very easy to separate it while using output array.
http://php.net/manual/en/function.array-unique.php