4

I have a little problem in php, which i find hard to explain in words. I have an associative array which contains key-value. I would like to make a function (or if there is already one) which would take an array as input and remove the duplicates but both ways.

For example:

In my array I have {a -> b} {a -> c} {b -> a} {b -> c} ...

From this view it does not seem like there is any duplicate, but to me {a -> b} and {b -> a} are duplicate. So I would like the function to see it as a duplicate and only return one of them.

I tried to use array_flip / array_unique to exchange the key and the values, in a loop but didn't quite work.

Could you help to find a way to do this even if it is array with a large length? or if there is a php function which does it.

Help would be really appreciated, thanks.


There is code to illustrate the idea:

For an array which would be like that:

Array ( 
    [0] => Array ( [0] => a [1] => b)
    [1] => Array ( [0] => a [1] => c )
    [2] => Array ( [0] => b [1] => a )
    [3] => Array ( [0] => b [1] => c )
)
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
StringerB
  • 75
  • 1
  • 5
  • Could you give a little more detailed code example? – Miloš Đakonović Feb 23 '13 at 11:37
  • So you only want to remove pairs that are identical, i.e. `a->b` & `b->a`, but not `a->b` & `a->c` or `a->b` & `c->b`? – Alasdair Feb 23 '13 at 11:39
  • *{a -> b} {a -> c}* do you have multiple keys with the same name (not possible)..? If I understood correctly if key *a* also exists as a value for example *b => a* it's a duplicate for you? If not then [array_diff_assoc()](http://www.php.net/manual/en/function.array-diff-assoc.php) is probably the function you're looking for.. – kjetilh Feb 23 '13 at 11:42
  • exactly Alasdair, and Miloshio unfortunately i dont. – StringerB Feb 23 '13 at 12:14
  • `[0] => Array ( [0] => a [1] => b)` this is not an associative array, either your question is misleading or your example is wrong. I guess its the former, because you are talking about duplicate keys, which would not be possible. – Fabian Schmengler Feb 23 '13 at 12:57
  • the reason why it like that is because i want to be able to repeat the same key different times in the array. so to be able to have 'a' as key different times otherwise it wouldn't work I think because array keys are unique – StringerB Feb 23 '13 at 13:11
  • or if you have a way of repeating the same key different time in the array, that would make it easier for me to also demonstrate – StringerB Feb 23 '13 at 13:13

2 Answers2

4

This will remove your dublicates

foreach($array as $key => $value){
     if (isset($array[$key])){
        if(isset($array[$value])){
            if($array[$value] == $key){
                unset($array[$value]);
            }
        }
     }
 }
Antonio Papa
  • 1,596
  • 3
  • 20
  • 38
2

This should work:

function cleanArray($array)
{
   $newArray = array();
   foreach ($array as $key => $val) {
      if (isset($array[$val]) && $array[$val] == $key) {
         if (!isset($newArray[$key]) && !isset($newArray[$val])) {
            $newArray[$key] = $val; 
         }      
         unset($array[$key], $array[$val]);
      }
   }    
   return array_merge($array, $newArray);
}

Working example here.

hohner
  • 11,498
  • 8
  • 49
  • 84