2013-03-20 88 views
1

您好我有多维php数组,如下所示, 我只是想从这个数组中删除重复的对象。是他们无论如何要在PHP中做到这一点? 我需要删除重复的stdClass对象。从多维数组中删除重复的对象

Array 
(
    [0] => stdClass Object 
     (
      [term_id] => 5 
      [name] => 4x4 
      [slug] => 4x4 
     [term_group] => 0 
     [term_taxonomy_id] => 5 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

[1] => stdClass Object 
    (
     [term_id] => 4 
     [name] => Ultra High Performance 
     [slug] => ultra-high-performance 
     [term_group] => 0 
     [term_taxonomy_id] => 4 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

[2] => stdClass Object 
    (
     [term_id] => 5 
     [name] => 4x4 
     [slug] => 4x4 
     [term_group] => 0 
     [term_taxonomy_id] => 5 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

[3] => stdClass Object 
    (
     [term_id] => 4 
     [name] => Ultra High Performance 
     [slug] => ultra-high-performance 
     [term_group] => 0 
     [term_taxonomy_id] => 4 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

) 

回答

3

试试这个:

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

下面是完整的代码:

$array = array(array(
        "term_id" => 5, 
        "name" => "4x4", 
        "slug" => "4x4", 
        "term_group" => 0, 
        "term_taxonomy_id" => 5, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2, 
       ), 
       array(
        "term_id" => 4, 
        "name" => "Ultra High Performance", 
        "slug" => "ultra-high-performance", 
        "term_group" => 0, 
        "term_taxonomy_id" => 4, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2, 
       ), 
       array(
        "term_id" => 5, 
        "name" => "4x4", 
        "slug" => "4x4", 
        "term_group" => 0, 
        "term_taxonomy_id" => 5, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2 
       ), 
       array(
        "term_id" => 4, 
        "name" => "Ultra High Performance", 
        "slug" => "ultra-high-performance", 
        "term_group" => 0, 
        "term_taxonomy_id" => 4, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2 
       ) 
); 

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

echo "<pre>"; 
print_r($array); 

输出:

Array 
(
    [0] => Array 
     (
      [term_id] => 5 
      [name] => 4x4 
      [slug] => 4x4 
      [term_group] => 0 
      [term_taxonomy_id] => 5 
      [taxonomy] => ptd_vehicle_sub_cat 
      [description] => 0 
      [parent] => 0 
      [count] => 2 
     ) 

    [1] => Array 
     (
      [term_id] => 4 
      [name] => Ultra High Performance 
      [slug] => ultra-high-performance 
      [term_group] => 0 
      [term_taxonomy_id] => 4 
      [taxonomy] => ptd_vehicle_sub_cat 
      [description] => 0 
      [parent] => 0 
      [count] => 2 
     ) 

) 
0

检查出array_filter()函数。它应该为您提供一个良好的起点。

4

保持简单......所有这一切都需要的是:

$list = array(); 
foreach ($data as $item) { 
    isset($list[$item->term_id]) or $list[$item->term_id] = $item; 
} 

print_r($list); //duplicate removed 
+0

基本上这是我所需要的答案。简单。谢谢! – 2016-02-03 02:01:36

0

试试这个:

$array = array_intersect_key($array, array_unique(array_map('serialize', $array))); 

我的方法是优于@PrasanthBendra原因对象将无法重新创建。 :^)但我更喜欢@巴巴的方式。

0

你可以试试这个:

foreach($array as $key=>$obj) {  
$skey = implode(",",$obj); 
if(!isset($check[$skey])) { 
    $new_array[$key]=$obj; 
    $check[$skey]=1; 
} 
} 

这应该离开数组$ new_array没有重复。

这虽然是一个更好的方法:

$array = array_intersect_key($array, array_unique(array_map('serialize', $array)));