例如,我有两个字符串:
$str1 = "one two three";
$str2 = "one two two three three three";
在上述情况下,如何将三作为最终结果?
答案 0 :(得分:1)
试试这个
$str1 = "one two three";
$str2 = "one two two three three three";
$array1=explode(" ",$str1);
$array2=explode(" ",$str2);
$result=array_merge($array1,$array2);
$count=array_count_values($result);
arsort($count);
echo key($count);
答案 1 :(得分:0)
使用这段代码:
$arr = array_count_values(explode(" ", $yourstr));
asort($arr);
reset($array);
//get first val
echo current($array), PHP_EOL ;
//get first key
echo key($array), PHP_EOL ;
有关详细信息,请参阅:php manual
答案 2 :(得分:0)
您可以使用explode,并将值放在数组中。然后根据您创建的数组的索引获取所需的值。
答案 3 :(得分:0)
$words = "one two two three three three";
print_r( array_count_values(str_word_count($words, 1)) );
<强>输出强>
Array
(
[one] => 1
[two] => 2
[three] => 3
)
检查手册 str_word_count 和 array_count_values