How to remove all instances of duplicated values f

2020-02-28 15:50发布

问题:

I know there is array_unique function, but I want to remove duplicates. Is there a built-in function or do I have to roll my own.

Example input:

banna, banna, mango, mango, apple

Expected output:

apple

回答1:

You can use a combination of array_unique, array_diff_assoc and array_diff:

array_diff($arr, array_diff_assoc($arr, array_unique($arr)))


回答2:

You can use

$singleOccurences = array_keys(
    array_filter(
        array_count_values(
            array('banana', 'mango', 'banana', 'mango', 'apple' )
        ),
        function($val) {
            return $val === 1;
        }
    )
)

See

  • array_count_values — Counts all the values of an array
  • array_filter — Filters elements of an array using a callback function
  • array_keys — Return all the keys or a subset of the keys of an array
  • callbacks


回答3:

Just write your own simple foreach loop:

$used = array();    
$array = array("banna","banna","mango","mango","apple");

foreach($array as $arrayKey => $arrayValue){
    if(isset($used[$arrayValue])){
        unset($array[$used[$arrayValue]]);
        unset($array[$arrayKey]);
    }
    $used[$arrayValue] = $arrayKey;
}
var_dump($array); // array(1) { [4]=>  string(5) "apple" } 

have fun :)



回答4:

If you want to only leave values in the array that are already unique, rather than select one unique instance of each value, you will indeed have to roll your own. Built in functionality is just there to sanitise value sets, rather than filter.



回答5:

You want to remove any entries that have duplicates, so that you're left with only the entries that were unique in the list? Hmm it does sound like something you'll need to roll your own.



回答6:

There is no existing function; You'll have to do this in two passes, one to count the unique values and one to extract the unique values:

$count = array();
foreach ($values as $value) {
  if (array_key_exists($value, $count))
    ++$count[$value];
  else
    $count[$value] = 1;
}

$unique = array();
foreach ($count as $value => $count) {
  if ($count == 1)
    $unique[] = $value;
}


回答7:

The answer on top looks great, but on a side note: if you ever want to eliminate duplicates but leave the first one, using array_flip twice would be a pretty simple way to do so. array_flip(array_flip(x))



回答8:

Only partially relevant to this specific question - but I created this function from Gumbo's answer for multi dimensional arrays:

function get_default($array)
{
    $default = array_column($array, 'default', 'id');
    $array = array_diff($default, array_diff_assoc($default, array_unique($default)));

    return key($array);
}

In this example, I had cached statuses and each one other than the default was 0 (the default was 1). I index the default array from the IDs, and then turn it into a string. So to be clear - the returned result of this is the ID of the default status providing it's in the same part of the multi dimensional array and not the key of it



回答9:

PHP.net http://php.net/manual/en/function.array-unique.php

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Takes an input array and returns a new array without duplicate values.


New solution:


function remove_dupes(array $array){
    $ret_array = array();
    foreach($array as $key => $val){
        if(count(array_keys($val) > 1){
            continue;
        } else { 
            $ret_array[$key] = $val; 
        }
}


标签: php arrays