drq22639 2017-04-28 12:36
浏览 45
已采纳

Codeigniter:如何从索引名称指定的数组中删除重复元素

How to remove Duplicate Array element by specifying index like article_id That means there would no other element having same article id in array .

This is what i am getting

$data["related_post"]   =$CI->Article_model->get_related_post($SearchTag);

and

echo "<pre>";
 print_r($data["related_post"]);
echo "</pre>";

Output:

Array
(
    [0] => stdClass Object
        (
            [article_id] => 49
            [article_viewed] => 156
            [article_title] => Copy 1 column to another by mysql query
            [article_url] => copy-1-column-to-another-by-mysql-query
            [article_status] => active
            [tag_name] => mysql
        )

    [1] => stdClass Object
        (
            [article_id] => 49
            [article_viewed] => 156
            [article_title] => Copy 1 column to another by mysql query
            [article_url] => copy-1-column-to-another-by-mysql-query
            [article_status] => active
            [tag_name] =>  sql
        )

    [2] => stdClass Object
        (
            [article_id] => 49
            [article_viewed] => 156
            [article_title] => Copy 1 column to another by mysql query
            [article_url] => copy-1-column-to-another-by-mysql-query
            [article_status] => active
            [tag_name] =>  unique-key
        )

)
  • 写回答

1条回答 默认 最新

  • duanlongling5308 2017-05-04 12:23
    关注
    function get_keys_for_duplicate_values($my_arr, $clean = false) {
        if ($clean) {
            return array_unique($my_arr);
        }
    
        $dups = $new_arr = array();
        foreach ($my_arr as $key => $val) {
          if (!isset($new_arr[$val])) {
             $new_arr[$val] = $key;
          } else {
            if (isset($dups[$val])) {
               $dups[$val][] = $key;
            } else {
               $dups[$val] = array($key);
               // Comment out the previous line, and uncomment the following line to
               // include the initial key in the dups array.
               // $dups[$val] = array($new_arr[$val], $key);
            }
          }
        }
        return $dups;
    }
    

    Source : [https://stackoverflow.com/a/6461034/7891160][1]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置