如何从PHP重新访问的多维数组中删除重复值

时间:2021-12-21 01:16:53

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

如何从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)));

根据如何从PHP建议中删除多维数组中的重复值我使用$ input = array_map(“unserialize”,array_unique(array_map(“serialize”,$ input)));

Array After performing array_unique():

数组执行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.

但仍然得到重复的答案。似乎对原始阵列没有影响。

2 个解决方案

#1


1  

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

首先,从5.2.9开始,你可以使用更简单的版本,取自这个答案:

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_unique()实际上给出了正确的输出;这里的事情是你的数组中有两个不同的键:“follower_userid”和“following_userid”,所以要获得唯一的id而不管键,你必须先将它标准化:

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_unique(array_map('current', $array));

#2


0  

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

如果它总是只有follower_userid,你可以用这样的东西压扁数组:

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

$followers = array_unique($followers);

#1


1  

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

首先,从5.2.9开始,你可以使用更简单的版本,取自这个答案:

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_unique()实际上给出了正确的输出;这里的事情是你的数组中有两个不同的键:“follower_userid”和“following_userid”,所以要获得唯一的id而不管键,你必须先将它标准化:

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_unique(array_map('current', $array));

#2


0  

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

如果它总是只有follower_userid,你可以用这样的东西压扁数组:

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

$followers = array_unique($followers);