如何从基于特定值的关联数组删除重复值? [英] How to remove duplicate values from an associative array based on a specific value?

查看:133
本文介绍了如何从基于特定值的关联数组删除重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,它看起来就像是:

 阵列(3){[fk_article_id] =>字符串(1)4[如first_name] =>字符串(6)乌尔里克[姓氏] =>串(10)Grasberger}阵列(3){[fk_article_id] =>字符串(1)9[如first_name] =>串(5)弗兰克[姓氏] =>串(9)Neubacher}阵列(3){[fk_article_id] =>串(3)896[如first_name] =>串(2)D。 [姓氏] =>串(5)鲍尔}阵列(3){[fk_article_id] =>串(3)896[如first_name] =>字符串(2)S [姓氏] =>串(6)的Anders}阵列(3){[fk_article_id] =>串(3)896[如first_name] =>字符串(2)M [姓氏] =>串(6)Tsokos}阵列(3){[fk_article_id] =>串(3)897[如first_name] =>字符串(8)莱因哈德[姓氏] =>串(8)Scholzen}

现在我想要做的就是摆脱重复的fk_article_id896,使得只有第一那些是左

 阵列(3){[fk_article_id] =>串(3)896[如first_name] =>串(2)D。 [姓氏] =>串(5)鲍尔}

我现在 array_unique(),但我还没有找到一种可能性,告诉函数只有在fk_article_id使用的值 ID。

我怎样才能做到这一点?

编辑:

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

  $ authors_result = pg_query($ query_authors)或trigger_error(发生错误< BR />中mysql_error()< BR /> SQL的陈述:{$ searchSQL});而($行= pg_fetch_assoc($ authors_result)){
后续代码var_dump($行);
}


解决方案

使用 一个快速的方法array_reduce 看起来像:

  $ =独特array_reduce($主题,功能($最终,$文章){
    静态$见过=阵列();
    如果(!array_key_exists($文章['fk_article_id'],$可见)){
        $见过[$文章['fk_article_id'] = NULL;
        $最后的[] = $物品;
    }
    返回$决赛;
});

尝试工作的例子


编辑:看来你不想要的结果汇总工作。这里有另外​​两个想法:

过滤在PHP

  $ =可见阵列();
而($行= pg_fetch_assoc($ authors_result)){
    //跳过此行,如果我们已经看到它的文章编号
    如果(array_key_exists($行['fk_article_id'],$见过)){
        继续;
    }
    //请注意,我们已经看到了这篇文章
    $见过[$行['fk_article_id'] = NULL;
    //做任何你行
    后续代码var_dump($行);
}

过滤在DB

这里的想法是要改变正在执行的查询,使得重复文章id不一定在结果集中出现。如何做到这一点将取决于您已经使用查询。

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

解决方案

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.

这篇关于如何从基于特定值的关联数组删除重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆