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
    )

)
4

5 回答 5

4

保持简单......所需要的只是:

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

print_r($list); //duplicate removed 
于 2013-03-20T09:05:27.673 回答
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
        )

)
于 2013-03-20T09:10:10.303 回答
0

查看array_filter()功能。它应该为您提供一个良好的起点。

于 2013-03-20T09:04:10.433 回答
0

试试这个:

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

我的方式比@PrasanthBendra 更好,因为对象不会重新创建。:^) 但我更喜欢@Baba 方式。

于 2013-03-20T09:15:23.103 回答
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)));
于 2013-03-20T09:18:40.060 回答