找到具有相同值的数组键 [英] Find all array keys that has same value
问题描述
有没有更简单的方式来获得具有相同值的数组键,当值是未知的。
的问题的有 array_unique
是返回独特的阵列,因此它没有找到唯一的值。
即,例如,从这个数组:
阵列(
[α] =&GT 1000
并[b] =→1
[C] => 1000
)
我要得到这个
阵列(
[α] =&GT 1000
[C] => 1000
)
解决这个另一种方式是,如果我能找到的寂寞的值,然后他们的钥匙,然后用和array_diff
这就是我这么远,看起来很恐怖:
$ a =阵列('A'=> 1000,'B'=大于1,'C'=> 1000);
$ B = array_flip(array_count_values($ A));
krsort($ B);
$最后= array_keys($ A,array_shift($ B));
更新结果
使用保罗Freites的回答为code的基础上,我可以得到它轻松的工作pretty,维护,容易对眼睛的一种方式......通过过滤作为静态类方法,我可以从重复值通过只调用数组类名:: get_duplicates($ array_to_filter)
私有静态$数= NULL;私人静态函数filter_duplicates($值){
返回self :: $计数[$值> 1;
}公共静态函数get_duplicates($数组){
自:: $数= array_count_values($数组);
返回array_filter($阵列,'类名:: filter_duplicates');
}
考虑关闭的优势更简单的解决方案:
$阵列=阵列('A'=> 1000,'B'=大于1,'C'=> 1000);
$数= array_count_values($数组);
$ =过滤array_filter($阵列功能($值)使用($计数){
返回$计数[$值> 1;
});
后续代码var_dump($过滤);
这给了我以下内容:
阵列(2){
[一] =>
INT(1000)
[C] =>
INT(1000)
}
这就是全部! :)
更新:向后兼容的解决方案
$阵列=阵列('A'=> 1000年,'B'=大于1,'C'=> 1000);
$数= array_count_values($数组);
$ =过滤array_filter($阵列,create_function('$值',
全球$计数;返回$计数[$值> 1;'));
后续代码var_dump($过滤);
Is there a simpler way to get all array keys that has same value, when the value is unknown.
The problem with array_unique
is that it returns the unique array and thus it doesn't find unique values.
That is, for example, from this array:
Array (
[a]=>1000
[b]=>1
[c]=>1000
)
I want to get this
Array (
[a]=>1000
[c]=>1000
)
Another way around this is, if I could find the lonely values, and then their keys, and then use array_diff
This is what I've got so far, looks awful:
$a = array( 'a' => 1000, 'b' => 1, 'c' => 1000 );
$b = array_flip( array_count_values( $a ) );
krsort( $b );
$final = array_keys( $a, array_shift( $b ) );
Update
Using Paulo Freites' answer as a code base, I could get it working pretty easily, maintainable and easy on eyes kind of way… by using the filtering as a static class method I can get the duplicate values from an array by just calling ClassName::get_duplicates($array_to_filter)
private static $counts = null;
private static function filter_duplicates ($value) {
return self::$counts[ $value ] > 1;
}
public static function get_duplicates ($array) {
self::$counts = array_count_values( $array );
return array_filter( $array, 'ClassName::filter_duplicates' );
}
Taking advantage of closures for a more straightforward solution:
$array = array('a' => 1000, 'b' => 1, 'c' => 1000);
$counts = array_count_values($array);
$filtered = array_filter($array, function ($value) use ($counts) {
return $counts[$value] > 1;
});
var_dump($filtered);
This gave me the following:
array(2) {
["a"]=>
int(1000)
["c"]=>
int(1000)
}
Demo: https://eval.in/67526
That's all! :)
Update: backward-compatible solution
$array = array('a' => 1000, 'b' => 1, 'c' => 1000);
$counts = array_count_values($array);
$filtered = array_filter($array, create_function('$value',
'global $counts; return $counts[$value] > 1;'));
var_dump($filtered);
Demo: https://eval.in/68255
这篇关于找到具有相同值的数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!