例如,我有两个字符串:
$str1 = "one two three";
$str2 = "one two two three three three";
在上述情况下,我如何获得三个作为最终结果?
尝试这个
$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);
您可以使用爆炸,并将值放入数组中。然后根据创建的数组的索引获取所需的值。
使用这段代码:
$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手动
$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