Asked  7 Months ago    Answers:  3   Viewed   90 times

How can I remove duplicate values from a multi-dimensional array in PHP?

Example array:

Array
(
    [0] => Array
    (
        [0] => abc
        [1] => def
    )

    [1] => Array
    (
        [0] => ghi
        [1] => jkl
    )

    [2] => Array
    (
        [0] => mno
        [1] => pql
    )

    [3] => Array
    (
        [0] => abc
        [1] => def
    )

    [4] => Array
    (
        [0] => ghi
        [1] => jkl
    )

    [5] => Array
    (
        [0] => mno
        [1] => pql
    )

)

 Answers

68

Here is another way. No intermediate variables are saved.

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Wednesday, March 31, 2021
 
DCD
answered 7 Months ago
DCD
30

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,
  ),
)
*/
Wednesday, March 31, 2021
 
sunshinejr
answered 7 Months ago
81

First of all, since 5.2.9 you can use the much simpler version, taken from this answer:

array_unique($array, SORT_REGULAR);

In this case, array_unique() actually gives the correct output; the thing here is that you have two different keys in your array: "follower_userid" and "following_userid", so to get the unique id's regardless of the keys, you have to normalize it first:

array_map('current', $array);

Doing this takes the first element of each array and creates a new array with just the value of that first element.

Output:

Array
(
    [0] => 88
    [1] => 89
    [2] => 287
    [3] => 346
    [4] => 405
    [5] => 284
    [6] => 583
    [7] => 587
    [8] => 655
    [9] => 95
    [10] => 89
    [11] => 88
    [12] => 353
    [13] => 42
    [14] => 626
    [15] => 655
    [16] => 95
);

And then apply array_unique():

array_unique(array_map('current', $array));
Wednesday, March 31, 2021
 
rblarsen
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :