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
939 views
in Technique[技术] by (71.8m points)

php - How to remove duplicate values from a multidimensional array?

I have array data from two separate mysql queries. Array data looks like below:

0
:
{user_id: 82, ac_type: 1,…}
1
:
{user_id: 80, ac_type: 5,…}
2
:
{user_id: 76, ac_type: 1,…}
3
:
{user_id: 82, ac_type: 1,…}
4
:
{user_id: 80, ac_type: 5,…}

I want to remove the duplicate array items.

So, my output will be like this:

0
:
{user_id: 82, ac_type: 1,…}
1
:
{user_id: 80, ac_type: 5,…}
2
:
{user_id: 76, ac_type: 1,…}

I want to check duplicate by user_id.

I have tried the following solutions, but neither are not working as desired.

$input = array_unique($res, SORT_REGULAR);

$input = array_map("unserialize", array_unique(array_map("serialize", $res)));

I also tried below.

$results = array();

foreach ($res as $k => $v) {
        $results[implode($v)] = $v;
}

$results = array_values($results);
print_r($results);

But still duplicate data exist.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You don't need any custom function calls for this task. Just use array_column() to assign new temporary keys to each subarray, then reindex the subarrays and print to screen. *this does overwrite earlier occurrences with later ones.

Code: (Demo)

$array=[
    ['user_id'=>82,'ac_type'=>1],
    ['user_id'=>80,'ac_type'=>5],
    ['user_id'=>76,'ac_type'=>1],
    ['user_id'=>82,'ac_type'=>2],
    ['user_id'=>80,'ac_type'=>6]
];
var_export(array_values(array_column($array,NULL,'user_id')));

Output:

array (
  0 => 
  array (
    'user_id' => 82,
    'ac_type' => 2,
  ),
  1 => 
  array (
    'user_id' => 80,
    'ac_type' => 6,
  ),
  2 => 
  array (
    'user_id' => 76,
    'ac_type' => 1,
  ),
)

If you want to keep the first occurrences and ignore all subsequent duplicates, this will do:

Code: (Demo) (* this keeps the first occurrences)

$array=[
    ['user_id'=>82,'ac_type'=>1],
    ['user_id'=>80,'ac_type'=>5],
    ['user_id'=>76,'ac_type'=>1],
    ['user_id'=>82,'ac_type'=>2],
    ['user_id'=>80,'ac_type'=>6]
];

foreach($array as $a){
    if(!isset($result[$a['user_id']])){
        $result[$a['user_id']]=$a;      // only store first occurence of user_id
    }
}
var_export(array_values($result));  // re-index

Output:

array (
  0 => 
  array (
    'user_id' => 82,
    'ac_type' => 1,
  ),
  1 => 
  array (
    'user_id' => 80,
    'ac_type' => 5,
  ),
  2 => 
  array (
    'user_id' => 76,
    'ac_type' => 1,
  ),
)

If you don't mind losing the order of your subarrays, you can use array_reverse() with array_column() to retain the first occurrences using temporary keys, then use array_values() to re-index:

var_export(array_values(array_column(array_reverse($array),NULL,'user_id')));
/*
array (
  0 => 
  array (
    'user_id' => 76,
    'ac_type' => 1,
  ),
  1 => 
  array (
    'user_id' => 82,
    'ac_type' => 1,
  ),
  2 => 
  array (
    'user_id' => 80,
    'ac_type' => 5,
  ),
)
*/

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

...