apoorv
apoorv

Reputation: 123

Remove Duplicates Entries Inside an Multidimensional Array

Hi I have php array I am running a foreach Inside a While Loop

    if ( $the_query->have_posts() ) { 

    while ($the_query->have_posts()) { 
        $the_query->the_post();
        $get_the_ids[]= get_the_ID();


        $terms = wp_get_post_terms( get_the_ID(), 'wpcm_alimentazione',array("fields" => "all") );


        foreach ($terms as $key_term => $value_term) {
            $post_data.= "<option value='{$value_term->term_id}'>{$value_term->name}</option>";
        }


        //

    }


}

print_r($post_data);

And the output of ther terms variables is like this

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 15
            [name] => Gasolina
            [slug] => gasolina
            [term_group] => 0
            [term_taxonomy_id] => 15
            [taxonomy] => wpcm_alimentazione
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)
Array
(
    [0] => WP_Term Object
        (
            [term_id] => 17
            [name] => Diesel
            [slug] => diesel
            [term_group] => 0
            [term_taxonomy_id] => 17
            [taxonomy] => wpcm_alimentazione
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )
[1] => WP_Term Object
        (
            [term_id] => 25
            [name] => LPG
            [slug] => LPG
            [term_group] => 0
            [term_taxonomy_id] => 25
            [taxonomy] => wpcm_alimentazione
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)
Array
(
    [0] => WP_Term Object
        (
            [term_id] => 17
            [name] => Diesel
            [slug] => diesel
            [term_group] => 0
            [term_taxonomy_id] => 17
            [taxonomy] => wpcm_alimentazione
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

Now I am getting duplicate Entries for Diesel so it is being printed twice I dont want that I want to remove the Duplicate Entries I have tried array_unique But since it is an object inside an array It doesn't work I have also tried

$term_id =  get_the_ID();
if($term_id ==  get_the_ID()){
continue;
}

At the start and end of the loop.But it does Not work as well I want to skip the duplicate value and print the next one.

Upvotes: 0

Views: 178

Answers (1)

Sameer
Sameer

Reputation: 371

Have a separate array to maintain terms_ids to check for duplicates

$terms_array = array();

While looping it and printing it,

Just check if it exists in $terms_array

while ($the_query->have_posts()) {
if(in_array($term_id, $terms_array))
{
   continue; 
}
just add that term_id to an array after checking
$terms_array[$term_id] = $term_id;

// All your normal code here
}

Upvotes: 1

Related Questions