获取数组重复值的数组 [英] Get an array of duplicate array values

查看:128
本文介绍了获取数组重复值的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里我测试重复值的数组。我想只得到重复的值数组,要给出错误信息给用户,并指出这是违规的值。我试过

  $ =重复和array_diff($ array_with_dupes,array_unique($ array_with_dupes));

但是,这并没有返回只重复的值 - 而不是我得到一个空数组。

什么是简单的方法来做到这一点?


解决方案

  $ ARR =阵列('A','A','B','C','D' ,'D','E');
$ arr_unique = array_unique($ ARR);
$ arr_duplicates和array_diff_assoc =($改编,$ arr_unique);
的print_r($ arr_duplicates);

以上将返回

 阵列

    [1] =>一个
    [5] => ð

I have an array where I'm testing for duplicate values. I want to get an array of only the duplicate values, to give an error message to the user, noting which are the offending values. I tried

$duplicates = array_diff( $array_with_dupes, array_unique($array_with_dupes) );

But that didn't return the only the duplicate values -- instead I got an empty array.

What's a simple way to do this?

解决方案

$arr = array('a','a','b','c','d','d','e');
$arr_unique = array_unique($arr);
$arr_duplicates = array_diff_assoc($arr, $arr_unique);
print_r($arr_duplicates);

The above will return

Array
(
    [1] => a
    [5] => d
)

这篇关于获取数组重复值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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