Kamaal ABOOTHALIB
Kamaal ABOOTHALIB

Reputation: 3548

Remove duplicate objects from multi dimensional array

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

Answers (5)

Gung Foo
Gung Foo

Reputation: 13558

Check out the array_filter() function. It should provide you with a good starting point.

Upvotes: 0

karmafunk
karmafunk

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

sectus
sectus

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

Prasanth Bendra
Prasanth Bendra

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

Baba
Baba

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

Related Questions