doujumiao5024 2015-04-21 16:51
浏览 43

从通过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?

  • 写回答

3条回答 默认 最新

  • dongzhi5587 2015-04-21 17:23
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。