如何根据特定的值从关联数组中删除重复的值?

时间:2022-06-12 04:00:05

I have an array that looks just like that:

我有一个像这样的数组:

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

array(3) { ["fk_article_id"]=> string(1) "9" ["first_name"]=> string(5) "Frank" ["last_name"]=> string(9) "Neubacher" }

array(3) { ["fk_article_id"]=> string(3) "896" ["first_name"]=> string(2) "D." ["last_name"]=> string(5) "Bauer" }

array(3) { ["fk_article_id"]=> string(3) "896" ["first_name"]=> string(2) "S." ["last_name"]=> string(6) "Anders" }

array(3) { ["fk_article_id"]=> string(3) "896" ["first_name"]=> string(2) "M." ["last_name"]=> string(6) "Tsokos" }

array(3) { ["fk_article_id"]=> string(3) "897" ["first_name"]=> string(8) "Reinhard" ["last_name"]=> string(8) "Scholzen" }

Now what I want to do is to get rid of the duplicate "fk_article_id" values "896", so that only the first of those is left:

现在我要做的是去掉"fk_article_id"的重复值"896",这样就只剩下第一个了:

array(3) { ["fk_article_id"]=> string(3) "896" ["first_name"]=> string(2) "D." ["last_name"]=> string(5) "Bauer" }

I now array_unique() but I haven't found a possibility to tell the function to only use the values in the "fk_article_id" ID.

我现在使用array_unique(),但是我还没有找到可能告诉函数只使用“fk_article_id”ID中的值。

How can I do this?

我该怎么做呢?

Edit:

编辑:

It's the output of a three column db table via:

它是一个三列db表的输出,通过:

$authors_result = pg_query($query_authors) or trigger_error("An error occurred.<br/>" . mysql_error() . "<br />SQL-Statements: {$searchSQL}");

while ($row = pg_fetch_assoc($authors_result)) {
var_dump($row);
}

5 个解决方案

#1


3  

A quick way using array_reduce would look like:

使用array_reduce的快速方法如下:

$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

在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

在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.

这里的想法是修改正在执行的查询,这样重复的文章id就不会出现在结果集中。

#2


1  

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

沿着这些线应该做的事情,我不认为有一个预先存在的数组函数。

#3


1  

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

一种方法是遍历数组,在一个新数组中完整地复制每个fk_article_id的第一个实例。

<?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;
  }
}

#4


0  

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.

我将使用foreach-loop循环遍历数组,并将所有没有重复_id的数组保存到新数组中。

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

#5


0  

** 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.

#1


3  

A quick way using array_reduce would look like:

使用array_reduce的快速方法如下:

$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

在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

在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.

这里的想法是修改正在执行的查询,这样重复的文章id就不会出现在结果集中。

#2


1  

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

沿着这些线应该做的事情,我不认为有一个预先存在的数组函数。

#3


1  

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

一种方法是遍历数组,在一个新数组中完整地复制每个fk_article_id的第一个实例。

<?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;
  }
}

#4


0  

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.

我将使用foreach-loop循环遍历数组,并将所有没有重复_id的数组保存到新数组中。

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

#5


0  

** 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.