检查并返回重复阵列PHP [英] Check and return duplicates array php

查看:106
本文介绍了检查并返回重复阵列PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的数组有任何重复,并在返回数组中重复值。
我想这是尽可能高效。

I would like to check if my array has any duplicates and return the duplicated values in an array. I want this to be as efficient as possible.

Example :$array = array(1,2,2,4,5)
function returndup($array) should return 2 ;

if array is array(1,2,1,2,5);
it should return an array with 1,2

另外,初始阵列始终是5个位置长

Also the initial array is always 5 positions long

推荐答案

这会比和array_diff快〜100倍。

this will be ~100 times faster than array_diff

$dups = array();
foreach(array_count_values($arr) as $val => $c)
    if($c > 1) $dups[] = $val;

这篇关于检查并返回重复阵列PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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