我知道以下代码
<?php
$array = array("1", "hello", "1", "world", "hello");
print_r(array_count_values($array));
?>
将输出:
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
我想检索重复次数最多的值,或者如果有多个重复实例次数最多的匹配实例,例如“ hello”和“ 1”将它们随机化,而不是检索第一个。
$array = array(1, "hello", 1, "world", "hello",'2','2','2','3','3','3');
$s = array_count_values($array);
$mostRepeated = max($s);
$s = array_filter( $s, function($v) use ($mostRepeated) {
return $v==$mostRepeated;
});
print_r($s);
echo array_rand($s); // randomized
步:
array_rand
请注意,只要有一个或多个具有相同值计数的条目,我就将此函数编写为透明的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句