Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

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

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...