从多维数组中删除重复的键

时间:2012-09-28 10:11:55

标签: php array-unique

我有一个多维对象数组:

  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 ...

我希望从这个数组中删除重复的数组(在这种情况下,我将删除1,只有0和3,因为0和1是相同的):

      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 ...

我用array_unique,array_keys,array_keys_exists尝试了很多东西......

例如:

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

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

4 个答案:

答案 0 :(得分:3)

此功能应该:

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

答案 1 :(得分:0)

你可以尝试

$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);

输出

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]

答案 2 :(得分:0)

    <?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) {
  }
}

 */

答案 3 :(得分:0)

如果我理解你的问题是正确的,你想要在所有键的基础上删除子阵列。

假设您在基础上有一个基于0的自然数组键:

$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];
}

所以

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

你得到:

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

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

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

)
相关问题