从多维数组中删除重复对象



嗨,我有多维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
    )
)

保持简单…所需要的是:

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

试试这个:

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

试试这个:

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

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

你可以试试:

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

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

相关内容

  • 没有找到相关文章

最新更新