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

前端 未结 5 1861
星月不相逢
星月不相逢 2020-12-20 01:53

I have an array that looks just like that:

array(3) { [\"fk_article_id\"]=> string(1) \"4\" [\"first_name\"]=> string(6) \"Ulrike\" [\"last_name\"]=>         


        
相关标签:
5条回答
  • 2020-12-20 02:19

    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.

    0 讨论(0)
  • 2020-12-20 02:21
    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

    0 讨论(0)
  • 2020-12-20 02:24

    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;
    }
    
    0 讨论(0)
  • 2020-12-20 02:25

    ** 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.
    
    0 讨论(0)
  • 2020-12-20 02:30

    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;
      }
    }
    
    0 讨论(0)
提交回复
热议问题