Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Trying to remove duplicate array elements without using array_unique. I am trying to use the array_flip method but that doesn't seem to be working for me. The Code that I have so far is:

$arr = array(
   'a' => "one",
   'b' => "two",
   'c' => "three",
   'd' => "two",
   'e' => "four",
   'f' => "five",
   'g' => "three",
   'h' => "two"
);

function removeDuplicates($arr) {


$arr = array_flip($arr);
$arr = array_flip($arr);

}
print_r(removeDuplicates($arr));

The output that I am trying to get ultimately is:

  [a] => one
    [e] => four
    [f] => five

This is just purely for educational purposes as I am trying to find different ways to do it. Any help is great appreciated.

share|improve this question
    
What happened to "two", and "three"? Do you want to remove duplicate elements, or elements that have duplicates? –  Rocket Hazmat May 30 '12 at 19:56
    
Your removeDuplicates needs a return $arr;. –  Rocket Hazmat May 30 '12 at 19:56
    
php manual "array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys." from php.net/manual/en/function.array-flip.php –  Efthimis May 30 '12 at 19:56
    
@EfthimisCharitonidis: yes, and? –  Rocket Hazmat May 30 '12 at 19:57
    
@Rocket He wants to remove the duplicate values? not exchange the pointers with the value or vice versa –  Efthimis May 30 '12 at 20:00
show 2 more comments

3 Answers

up vote 2 down vote accepted

Another solution would be:

$counts = array_count_values($arr);

foreach ($arr as $key => $value) {
        if ($counts[$value] > 1) {
                unset($arr[$key]);
        }
}

Also, array_unique makes sure there are no duplicate values, while you want the array without the values which had duplicates. That is quite different, I guess removing mention of the array_unique function will make the question less confusing.

share|improve this answer
    
+1 for array_count_values. –  Rocket Hazmat May 30 '12 at 20:18
add comment

If you added return $arr; to your code, it'd act the same as array_unique. Well, almost. array_unique would keep the 1st instance of an element, and yours would keep the last.

Your example shows you want to remove elements that have duplicates not duplicate elements. For that, you'd need to get a list of duplicate entries, then remove those values.

UPDATE: Check out array_filter, this should work for your situation.

// This only works in PHP 5.3+
$arr = array_filter($arr, function($a) use($arr){
    // remove the element if it exists more than once
    return count(array_keys($arr, $a)) === 1; // Get the keys for a value
});

DEMO: http://codepad.viper-7.com/l8JkSh

share|improve this answer
add comment

array_flip will ensure that you don't have any duplicates, but it won't give you the unique values.

The output you are looking for

[a] => one
[e] => four
[f] => five

is unique values. The array you are probably getting is

[a] => one
[b] => two
[c] => three
[e] => four
[f] => five

For your output you would need to do something like

function array_real_unique($arr, $strict = FALSE) {
    $unique = array();
    $dupes = array();
    foreach ($arr as $key => $value) {
        if (($index = array_search($value, $dupes, $strict)) === FALSE)
            $unique[$key] = $value;
        else
            unset($unique[$index]);
        $dupes[$key] = $value;
    }

    return $unique;
}
share|improve this answer
    
+1, good solution. Though $dupes isn't really needed. You can check if $index === $key instead, as array_search returns the 1st match. codepad.org/P7erfwYD –  Rocket Hazmat May 30 '12 at 21:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.