How to remove duplicate values from an associative array based on a specific value?

末鹿安然 提交于 2019-11-29 12:17:22

A quick way using array_reduce would look like:

$unique = array_reduce($subject, function($final, $article){
    static $seen = array();
    if ( ! array_key_exists($article['fk_article_id'], $seen)) {
        $seen[$article['fk_article_id']] = NULL;
        $final[] = $article;
    }
    return $final;
});

Try a working example.


Edit: Seems you don't want to work with the aggregated results. Here are two other thoughts:

Filtering in PHP

$seen = array();
while ($row = pg_fetch_assoc($authors_result)) {
    // Skip this row if we have already seen its article id
    if (array_key_exists($row['fk_article_id'], $seen)) {
        continue;
    }
    // Note that we have seen this article
    $seen[$row['fk_article_id']] = NULL;
    // Do whatever with your row
    var_dump($row);
}

Filtering in the DB

The idea here is to change the query being executed so that the repeated article ids do not appear in the result set. How this is done will depend on the query that you're already using.

foreach($array as $key => $value){
    if(!in_array( $array[$key]['fk_article_id'], $usedValues)){
        $usedValues = $array[$key]['fk_article_id'];
        $cleanArray = $array[$key];
    }
}

something along thoose lines should do it, i don't think there is an pre existing array funciton for that

One way would be to iterate over the array, copying ownly the first instance of each fk_article_id in a new array.

<?php

$your_unique_array = array();
foreach ($your_original_array as $v) {
  if (!isset($your_unique_array[$v['fk_article_id']) {
      $your_unique_array[$v['fk_article_id'] = $v;
  }
}

I would use a foreach-loop to iterate over the arrays and save all arrays that dont have a duplicate _id to a new array.

$clean = array();
foreach ($arrays as $array) {
  if (!isset($clean[$array['fk_article_id']])
    $clean[$array['fk_article_id']] = $array;
}

** get Unique Associative Array ** using this method you can get easily unique Associative array /Multidimensional Array.

  array:6 [▼
     0 => array:1 [▼
       2 => "Airtel DTH"
           ]
      1 => array:1 [▼
      2 => "Airtel DTH"
           ]
      2 => array:1 [▼
      2 => "Airtel DTH"
           ]
      3 => array:1 [▼
      3 => "NeuSoft PVT LMT"
      ]
       4 => array:1 [▼
      3  => "NeuSoft PVT LMT"
       ]
      5 => array:1 [▼
       3 => "NeuSoft PVT LMT"
       ]
      ]


     function  assoc_Array_unique($array)
            {
                $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

                foreach ($result as $key => $value)
                {
                    if ( is_array($value) )
                    {
                        $result[$key] = assoc_Array_unique($value);
                    }
                }
                return   $result;
            }
   $arr_Company=   assoc_Array_unique($arr_Company);  // get Unique Array 
                $arr_Company = array_values($arr_Company); Rearrange Index of Array.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!