从通过wordpress查询获取的多维对象数组中删除重复项

I am trying to get POST data based on a query, but I get duplicate results. I tried sort regular but still it shows many duplicates. What should I do?

My code (as shown below) prints repeated object IDs in the foreach loop. Here, object_id = 2423 repeats. I want to keep one and delete the other other.

$vbn = $wpdb->get_results($query_cat_filter);
$bbc = array_unique($vbn, SORT_REGULAR);

foreach( $bbc as $cOM){
    print_r($bbc);
}   

Output:

Array
(
    [0] => stdClass Object
        (
            [object_id] => 2415
            [term_taxonomy_id] => 242
            [term_order] => 0
        )

    [1] => stdClass Object
        (
            [object_id] => 2423
            [term_taxonomy_id] => 242
            [term_order] => 0
        )

    [2] => stdClass Object
        (
            [object_id] => 2423
            [term_taxonomy_id] => 312
            [term_order] => 0
        )
)

How can I remove these duplicate values?

There maybe be a native function to do that, however, when I want to organize by key, I use a function like so. Note: I haven't tried it on an object, so you may have to first convert your objects to array by using (array):

<?php
    function organize($array = array(), $assockey = false,$forcerows = false)
        {
            if(!empty($array)) {
                    $i = 0;
                    foreach($array as $rows) {
                            if(is_array($rows)) {
                                    foreach($rows as $key => $value) {
                                            if(!empty($rows[$assockey])) {
                                                $_key   =   $rows[$assockey];
                                                $new[$_key][$i][$key]   =   $value;
                                            }
                                        }
                                }
                            $i++;
                        }
                }
            if(isset($new)) {
                    foreach($new as $key => $value) {
                            if(count($value) == 1) {
                                    $keyName    =   array_keys($value);
                                    $new[$key]  =   ($forcerows == true)? array_values($value):$value[$keyName[0]];
                                }
                            else {
                                    $new[$key]  =   array_values($value);
                                }
                        }
                }

            return (isset($new))? $new : $array;
        }

 // Starting input      
 $vals  =   array(
            '0' => array(
                    'object_id' => 2415,
                    'term_taxonomy_id' => 242,
                    'term_order' => 0,),
            '1' => array(
                    'object_id' => 2423,
                    'term_taxonomy_id' => 242,
                    'term_order' => 0,
                ),
            '2' => array(
                    'object_id' => 2423,
                    'term_taxonomy_id' => 312,
                    'term_order' => 0
                )
            );

    // Organize by key          
    $arr    =   organize($vals,'object_id',true);
    print_r($arr);
?>

Gives you an array like so:

Array
(
    [2415] => Array
        (
            [0] => Array
                (
                    [object_id] => 2415
                    [term_taxonomy_id] => 242
                    [term_order] => 0
                )

        )

    [2423] => Array
        (
            [0] => Array
                (
                    [object_id] => 2423
                    [term_taxonomy_id] => 242
                    [term_order] => 0
                )

            [1] => Array
                (
                    [object_id] => 2423
                    [term_taxonomy_id] => 312
                    [term_order] => 0
                )

        )

)

From here, you can just use key [0] of each array.

$vbn = array(
    (object)array(
        'object_id' => 2135,
        'term_taxonomy' => 242,
        'term_order' => 0,
    ),
    (object)array(
        'object_id' => 3245,
        'term_taxonomy' => 312,
        'term_order' => 0,
    ),
    (object)array(
        'object_id' => 3245,
        'term_taxonomy' => 242,
        'term_order' => 0,
    ),
    (object)array(
        'object_id' => 5623,
        'term_taxonomy' => 714,
        'term_order' => 0,
    )
);

The new arrays could be made associative, for simplicity I went with numeric.

DEDUP CODE

foreach($vbn as $key => $obj){
 $value = get_object_vars($obj);
 $vbn2[$value['object_id']] = array($value['term_taxonomy'],$value['term_order']);
}

OR

foreach($vbn as $obj) {
    $result[$obj->object_id] = array($obj->term_taxonomy,$obj->term_order);
}

NEW FOREACH

foreach($vbn2 as $key => $value){
  echo "$key => $value[0], $value[1]
";
}

RESULT

2135 => 242, 0
3245 => 242, 0
5623 => 714, 0

var_export($vbn2);

array (
  2135 => 
  array (
    'term_taxonomy' => 242,
    'term_order' => 0,
  ),
  3245 => 
  array (
    'term_taxonomy' => 242,
    'term_order' => 0,
  ),
  5623 => 
  array (
    'term_taxonomy' => 714,
    'term_order' => 0,
  ),

ASSOCIATIVE

Results are same as above.

 var_export($vbn2);

foreach($vbn as $key => $obj){
 $value = get_object_vars($obj);
 $vbn2[$value['object_id']] = array('term_taxonomy'=>$value['term_taxonomy'],'term_order'=>$value['term_order']);
}
foreach($vbn2 as $key => $value){
  echo "$key => " . $value['term_taxonomy'] . ', ' . $value['term_order'] . "
";
}

array (
  2135 => 
  array (
    'term_taxonomy' => 242,
    'term_order' => 0,
  ),
  3245 => 
  array (
    'term_taxonomy' => 242,
    'term_order' => 0,
  ),
  5623 => 
  array (
    'term_taxonomy' => 714,
    'term_order' => 0,
  ),
)

If You Don't Mind an Ugly Mess

It dedups just the same but the result is ugly.

foreach($vbn as $obj) {
    $vbn2[$obj->object_id] = (object) array($obj->term_taxonomy,$obj->term_order);
 }  
 var_export($vbn2);  

Output:

array (
  2135 => 
  stdClass::__set_state(array(
     'term_taxonomy' => 242,
     'term_order' => 0,
  )),
  3245 => 
  stdClass::__set_state(array(
     'term_taxonomy' => 242,
     'term_order' => 0,
  )),
  5623 => 
  stdClass::__set_state(array(
     'term_taxonomy' => 714,
     'term_order' => 0,
  )),
)

OR if you don't mind wasting memory

foreach($vbn as $object) {
    $vbn2[$object->object_id] = $object;
}
var_export($vbn2);

output:

array (
  2135 => 
  stdClass::__set_state(array(
     'object_id' => 2135,
     'term_taxonomy' => 242,
     'term_order' => 0,
  )),
  3245 => 
  stdClass::__set_state(array(
     'object_id' => 3245,
     'term_taxonomy' => 242,
     'term_order' => 0,
  )),
  5623 => 
  stdClass::__set_state(array(
     'object_id' => 5623,
     'term_taxonomy' => 714,
     'term_order' => 0,
  )),
)

Simple enough:

foreach($vbn as $object) {
    $result[$object->object_id] = $object;
}

And if you want to reorder the keys afterward:

$result = array_values($result);

Alternately, if you don't mind getting arrays instead of objects, use ARRAY_A in get_results (PHP >= 5.5.0 needed for array_column):

$vbn = $wpdb->get_results($query_cat_filter, ARRAY_A);
$vbn = array_column($vbn, null, 'object_id');

//or for only `object_id`
$vbn = array_column($vbn, 'object_id');