Reputation: 3548
Hi i have multidimensional php array shown below, i just wanted to remove duplicated objects from this array. is their anyway to do this in php? all i need that remove the stdClass Object that repeating.
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
)
)
Upvotes: 1
Views: 2844
Reputation: 13558
Check out the array_filter()
function. It should provide you with a good starting point.
Upvotes: 0
Reputation: 1453
You could try this:
foreach($array as $key=>$obj) {
$skey = implode(",",$obj);
if(!isset($check[$skey])) {
$new_array[$key]=$obj;
$check[$skey]=1;
}
}
This should leave the array $new_array without duplicates.
This is a much better way though:
$array = array_intersect_key($array, array_unique(array_map('serialize', $array)));
Upvotes: 0
Reputation: 15464
Try this:
$array = array_intersect_key($array, array_unique(array_map('serialize', $array)));
My way is better than @PrasanthBendra cause objects will not recreate. :^ ) But i prefer @Baba way.
Upvotes: 0
Reputation: 32740
Try this :
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
Here is the complete code :
$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);
Output :
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
)
)
Upvotes: 3
Reputation: 95101
Keep it simple ... all that is needed is :
$list = array();
foreach ( $data as $item ) {
isset($list[$item->term_id]) or $list[$item->term_id] = $item;
}
print_r($list); //duplicate removed
Upvotes: 4