note 105169 modified in function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Aug 2011 13:46:31 +0000 |
| Subject: | note 105169 modified in function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182045@lists.php.net to get a copy of this message | ||
I required a function that removed a specific duplicate entry from an array and ignoring all others
so came up with this:
<?php
function specified_array_unique($array, $value)
{
$count = 0;
foreach($array as $array_key => $array_value)
{
if ( ($count > 0) && ($array_value == $value) )
{
unset($array[$array_key]);
}
if ($array_value == $value) $count++;
}
return array_filter($array);
}
?>
--was--
Sorry - I just spotted an error with the code I just posted - this is the correct version
<?php
function specified_array_unique($array, $value)
{
$count = 0;
foreach($array as $array_key => $array_value)
{
if ( ($count > 0) && ($array_value == $value) )
{
unset($array[$array_key]);
}
if ($array_value == $value) $count++;
}
return array_filter($array);
}
?>
http://php.net/manual/en/function.array-unique.php