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

时间:2014-03-03 05:35:11

标签: php arrays

如何从PHP中删除多维数组中的重复值?

初始数组:

    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',
  ),
)

根据我使用的How to remove duplicate values from a multi-dimensional array in PHP建议$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

数组执行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',
  ),
)

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

2 个答案:

答案 0 :(得分:1)

首先,从5.2.9开始,您可以使用更简单的版本,取自this answer

array_unique($array, SORT_REGULAR);

在这种情况下,array_unique()实际上给出了正确的输出;这里的事情是你的数组中有两个不同的键:“ follower _userid”和“ follow _userid”,所以要获得唯一的id而不管键,你必须先将其标准化:

array_map('current', $array);

这样做会获取每个数组的第一个元素,并创建一个只包含第一个元素值的新数组。

输出:

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
);

然后应用array_unique()

array_unique(array_map('current', $array));

答案 1 :(得分:0)

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

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

$followers = array_unique($followers);