Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm trying to remove duplicated entries where value and type are both equal on a multidimensional associative array, but only using recursion and without array_unique. All keys are associative.

I tried this, and I'm getting the same result as the main array. My logic seems to fail me at this late hour.

function rmDuplicates(&$array) {
  $uniqueArray = array();
  foreach($array as $k=>$v) {
     if (is_array($v)) {
       $uniqueArray[$k] = rmDuplicates($v);
     } else {
       if (!in_array($v, $uniqueArray)) {
         $uniqueArray[] = $v;
       }
     }
  }
  return $uniqueArray;
}
share|improve this question
    
Why not array_unique? – OIS Oct 6 '14 at 22:12
    
Can you post a sample of your input data? – lxg Oct 6 '14 at 22:15
    
possible duplicate of How to remove duplicate values from a multi-dimensional array in PHP – OIS Oct 6 '14 at 22:53
    
how it can be duplicate when I said that I don't want any array_unique? For educational purposes I just want a way without array_unique and with recursion – user2035693 Oct 7 '14 at 4:39
    
Some of those answers are without array_unique. – OIS Oct 7 '14 at 8:04

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.