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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程