如何从PHP中的多维数组重新删除重复值 [英] How to remove duplicate values from a multi-dimensional array in PHP revisited

查看:143
本文介绍了如何从PHP中的多维数组重新删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I remove duplicate values from a multi-dimensional array in 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',
  ),
)

由于每<一个href=\"http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php\">How除去在PHP 建议我用 $输入= array_map(反序列化,array_unique(array_map(连载,$输入)))从多维数组重复值;

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

推荐答案

首先,因为5.2.9可以使用更简单的版本,从的这个答案

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

array_unique($array, SORT_REGULAR);

在这种情况下, array_unique(),其实是给予正确的输出;这里的事情是,你有你的阵列中的两个不同的密钥:跟随的_userid和以下的_userid,因此要获得唯一的ID的不管钥匙,你必须首先正常化吧:

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.

输出:

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

这篇关于如何从PHP中的多维数组重新删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆