Question

I have a multidimensionnal array of objects:

  0 => 
    array
      32281 => object ...
      105145 => object ...
      165656 => object ...
      194124 => object ...
      195397 => object ...
      205859 => object ...
  1 => 
    array
      32281 => object ...
      91504 => object ...
      165656 => object ...
      194124 => object ...
      195397 => object ...
      205859 => object ...
  3 => 
    array
      32281 => object ...
      105145 => object ...
      165656 => object ...
      194124 => object ...
      195397 => object ...
      205859 => object ...

and I want to remove duplicate arrays from this array like this (in this case I will remove 1 and just have 0 and 3 because 0 and 1 are the same) :

      0 => 
        array
          32281 => object ...
          105145 => object ...
          165656 => object ...
          194124 => object ...
          195397 => object ...
          205859 => object ...
      3 => 
        array
          32281 => object ...
          91504 => object ...
          165656 => object ...
          194124 => object ...
          195397 => object ...
          205859 => object ...

I have tried unsuccessfully a lot of things with array_unique, array_keys, array_keys_exists...

for example :

$array = array_map("unserialize", array_unique(array_map("serialize", $array)));

or

 $result = array();     
 foreach ($array as $key => $value) { 
    if(!array_key_exists($key,$result))
        $result[$key] = $array[$key]; 
  }
Was it helpful?

Solution

This function should do:

function my_unique($array) {
  foreach($array as $key => $value) {
    foreach($array as $key2 => $value2) {
      if($key != $key2 && $value === $value2) {
        unset($array[$key]);
      }
    }
  }
  return $array;
}

OTHER TIPS

    <?php
$arr =  array (0 => 
    array(
      32281 => new stdClass,
      105145 => new stdClass,
      165656 => new stdClass,
      194124 => new stdClass,
      195397 => new stdClass,
      205859 => new stdClass,
    ),
  1 => 
    array(
      32281 => new stdClass,
      91504 => new stdClass,
      165656 => new stdClass,
      194124 => new stdClass,
      195397 => new stdClass,
      205859 => new stdClass,
    ),
  3 => 
    array(
      32281 => new stdClass,
      105145 => new stdClass,
      165656 => new stdClass,
      194124 => new stdClass,
      195397 => new stdClass,
      205859 => new stdClass,
    ),
);

$result = array();
function put($value, $key) {
    global $result;
    $result[$key] = $value;
}
array_walk_recursive($arr, "put");

var_dump($result);

/**
array(7) {
  [32281]=>
  object(stdClass)#13 (0) {
  }
  [105145]=>
  object(stdClass)#14 (0) {
  }
  [165656]=>
  object(stdClass)#15 (0) {
  }
  [194124]=>
  object(stdClass)#16 (0) {
  }
  [195397]=>
  object(stdClass)#17 (0) {
  }
  [205859]=>
  object(stdClass)#18 (0) {
  }
  [91504]=>
  object(stdClass)#8 (0) {
  }
}

 */

If I understand you question correctly, you want to de-duplicate sub-arrays bases on all keys.

Assuming you have a natural 0-based array key on the base:

$keys=array_map("serialize",array_map("array_keys",$arr));
$keys=array_unique($keys);
$result=array();
foreach($keys as $idx=>$not_care)
{
    $result[$idx]=$arr[$idx];
}

So for

$arr=array(array(1234=>"1234",5678=>"5678"),
    array(1357=>"1357",2468=>"2468"),
    array(1234=>"1234",5678=>"5678"),
    array(1357=>"1357",8642=>"8642"));

you get:

Array
(
    [0] => Array
        (
            [1234] => 1234
            [5678] => 5678
        )

    [1] => Array
        (
            [1357] => 1357
            [2468] => 2468
        )

    [3] => Array
        (
            [1357] => 1357
            [8642] => 8642
        )

)

You can try

$array = array(
  0 => 
    array (
      32281 => new stdClass , 
      105145 => new stdClass , 
      165656 => new stdClass , 
      194124 => new stdClass , 
      195397 => new stdClass , 
      205859 => new stdClass ),
  1 => 
    array (
      32281 => new stdClass , 
      91504 => new stdClass , 
      165656 => new stdClass , 
      194124 => new stdClass , 
      195397 => new stdClass , 
      205859 => new stdClass ) ,
  3 => 
    array (
      32281 => new stdClass , 
      105145 => new stdClass , 
      165656 => new stdClass , 
      194124 => new stdClass , 
      195397 => new stdClass , 
      205859 => new stdClass  )
    );


$array2 = array_unique(array_map("serialize", $array));
$final = array_map("unserialize",array_diff($array2, array_diff_assoc(array_map("serialize", $array), $array2)));
var_dump($final);

Output

array
  1 => 
    array
      32281 => 
        object(stdClass)[19]
      91504 => 
        object(stdClass)[20]
      165656 => 
        object(stdClass)[21]
      194124 => 
        object(stdClass)[22]
      195397 => 
        object(stdClass)[23]
      205859 => 
        object(stdClass)[24]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top