PHP获取数组中重复值的键值
$array = array (
0=>'a',
1=>'b',
2=>'a',
5=>'b',
6=>'c',
40=>'d'
);
$keyarr =[];
$resultkey = [];
foreach ($array as $k => $v) {
if (in_array($v, $keyarr)) {
//在数组中搜索键值$v,并返回它的键名
$resultkey[] = array_search($v,$keyarr);
$resultkey[] = $k;
}else{
$keyarr[] = $v;
}
}
print_r($resultkey);
最后的结果:
Array
(
[0] => 0
[1] => 2
[2] => 1
[3] => 5
)