1

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
    )

)
Kamaal ABOOTHALIB
  • 3,527
  • 1
  • 19
  • 25

5 Answers5

4

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 
Baba
  • 94,024
  • 28
  • 166
  • 217
3

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
        )

)
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

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

double-beep
  • 5,031
  • 17
  • 33
  • 41
Gung Foo
  • 13,392
  • 5
  • 31
  • 39
0

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.

sectus
  • 15,605
  • 5
  • 55
  • 97
0

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)));
karmafunk
  • 1,453
  • 12
  • 20