1

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;
}
user2035693
  • 193
  • 2
  • 16
  • Why not array_unique? – OIS Oct 06 '14 at 22:12
  • Can you post a sample of your input data? – lxg Oct 06 '14 at 22:15
  • possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – OIS Oct 06 '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 07 '14 at 04:39
  • Some of those answers are without array_unique. – OIS Oct 07 '14 at 08:04
  • look better. The ones without array_unique are just for maximum 2 dimensional array. For endless number of dimensions you need recursion. Because the question is similar doesn't mean is duplicate. I looked on all questions around here and no one answered of a way to do it completely without array_unique and using recusion – user2035693 Oct 07 '14 at 10:33

0 Answers0