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

发布于 2024-09-24 10:01:30 字数 1447 浏览 6 评论 0原文

我有一个看起来像这样的数组:

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" }

现在我想做的是删除重复的 "fk_article_id""896",这样只有第一个其中剩下:

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

我现在 array_unique() 但我还没有找到告诉函数仅使用 "fk_article_id" ID 中的值的可能性。

我该怎么做?

编辑:

它是三列数据库表的输出:

$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);
}

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:

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.

How can I do this?

Edit:

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

$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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蘸点软妹酱 2024-10-01 10:01:30

使用 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;
});

尝试 一个工作示例


编辑:似乎您不想使用汇总结果。这里还有另外两个想法:

在 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);
}

在数据库中过滤

这里的想法是更改正在执行的查询,以便重复的文章 id 不会出现在结果集中。如何完成此操作取决于您已经使用的查询。

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.

若水微香 2024-10-01 10:01:30
foreach($array as $key => $value){
    if(!in_array( $array[$key]['fk_article_id'], $usedValues)){
        $usedValues = $array[$key]['fk_article_id'];
        $cleanArray = $array[$key];
    }
}

沿着这些思路的东西应该可以做到这一点,我不认为有一个预先存在的数组函数可以做到这一点

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

弥繁 2024-10-01 10:01:30

我将使用 foreach 循环来迭代数组,并将所有没有重复 _id 的数组保存到新数组中。

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

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;
}
你怎么这么可爱啊 2024-10-01 10:01:30

一种方法是迭代数组,将每个 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;
  }
}

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;
  }
}
爱格式化 2024-10-01 10:01:30

** 获取唯一关联数组 **
使用这种方法,您可以轻松获得唯一的关联数组/多维数组。

  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.

** 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文