嗨我有多维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
)
)
答案 0 :(得分:4)
保持简单......所需要的只是:
$list = array();
foreach ( $data as $item ) {
isset($list[$item->term_id]) or $list[$item->term_id] = $item;
}
print_r($list); //duplicate removed
答案 1 :(得分: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
)
)
答案 2 :(得分:0)
查看array_filter()
功能。它应该为您提供一个良好的起点。
答案 3 :(得分:0)
试试这个:
$array = array_intersect_key($array, array_unique(array_map('serialize', $array)));
我的方式比@PrasanthBendra更好,因为对象不会重新创建。 :^)但我更喜欢@Baba方式。
答案 4 :(得分: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)));