4

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

Initial array:

    array (
  0 => 
  array (
    'following_userid' => '88',
  ),
  1 => 
  array (
    'following_userid' => '89',
  ),
  2 => 
  array (
    'following_userid' => '287',
  ),
  3 => 
  array (
    'following_userid' => '346',
  ),
  4 => 
  array (
    'following_userid' => '405',
  ),
  5 => 
  array (
    'following_userid' => '284',
  ),
  6 => 
  array (
    'following_userid' => '583',
  ),
  7 => 
  array (
    'following_userid' => '587',
  ),
  8 => 
  array (
    'following_userid' => '655',
  ),
  9 => 
  array (
    'following_userid' => '95',
  ),
  10 => 
  array (
    'follower_userid' => '89',
  ),
  11 => 
  array (
    'follower_userid' => '88',
  ),
  12 => 
  array (
    'follower_userid' => '353',
  ),
  13 => 
  array (
    'follower_userid' => '42',
  ),
  14 => 
  array (
    'follower_userid' => '626',
  ),
  15 => 
  array (
    'follower_userid' => '655',
  ),
  16 => 
  array (
    'follower_userid' => '95',
  ),
)

As per the How to remove duplicate values from a multi-dimensional array in PHP Suggestion i used $input = array_map("unserialize", array_unique(array_map("serialize", $input)));

Array After performing array_unique():

    array (
  0 => 
  array (
    'following_userid' => '88',
  ),
  1 => 
  array (
    'following_userid' => '89',
  ),
  2 => 
  array (
    'following_userid' => '287',
  ),
  3 => 
  array (
    'following_userid' => '346',
  ),
  4 => 
  array (
    'following_userid' => '405',
  ),
  5 => 
  array (
    'following_userid' => '284',
  ),
  6 => 
  array (
    'following_userid' => '583',
  ),
  7 => 
  array (
    'following_userid' => '587',
  ),
  8 => 
  array (
    'following_userid' => '655',
  ),
  9 => 
  array (
    'following_userid' => '95',
  ),
  10 => 
  array (
    'follower_userid' => '89',
  ),
  11 => 
  array (
    'follower_userid' => '88',
  ),
  12 => 
  array (
    'follower_userid' => '353',
  ),
  13 => 
  array (
    'follower_userid' => '42',
  ),
  14 => 
  array (
    'follower_userid' => '626',
  ),
  15 => 
  array (
    'follower_userid' => '655',
  ),
  16 => 
  array (
    'follower_userid' => '95',
  ),
)

But Still getting Duplicate answers. Seems to have no effect on the original array.

Community
  • 1
  • 1
Amit
  • 3,251
  • 3
  • 20
  • 31

2 Answers2

1

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));
Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

If it's always only going to have just the follower_userid, you could just flatten the array down with something like this:

$followers = array();
foreach ($input as $item) {
    $followers[] = $item['followers_userid'];
}

$followers = array_unique($followers);
duellsy
  • 8,497
  • 2
  • 36
  • 60