Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取字符串中最多的字母_Php - Fatal编程技术网

Php 获取字符串中最多的字母

Php 获取字符串中最多的字母,php,Php,我需要PHP函数,可以从字符串中获得最多的字母 $string = "111010111010001101"; $execute = SomeFunction($string); echo $execute; 输出将是这样的 1 有这样的php函数吗?谢谢你不是很有效而是很简单 max(str_split($string)); 可以,只要您的字符集不是字符串中包含多字节字符的多字节字符集。请参见手册页,了解在这里 $string = "111010111010001101"; //$st

我需要PHP函数,可以从字符串中获得最多的字母

$string = "111010111010001101";
$execute = SomeFunction($string);
echo $execute;
输出将是这样的

1

有这样的php函数吗?谢谢你

不是很有效而是很简单

max(str_split($string));

可以,只要您的字符集不是字符串中包含多字节字符的多字节字符集。请参见手册页,了解

在这里

$string = "111010111010001101";

//$string = "abaacabdeeeee";

$array_count = array_count_values(str_split($string));
$res = array_keys($array_count, max($array_count));

print_r($res);
要使其成为函数,只需执行以下操作:

function SomeFunction($string){
   $array_count = array_count_values(str_split($string));
   return array_keys($array_count, max($array_count));
}

print_r(SomeFunction('111010111010001101'));
输出

 1 //or e in the commented one I tested
Array
(
   [0] => a
   [1] => b
)

它的工作原理

  • 将字符串拆分为字符数组
    str\u Split
  • 计算字符
    数组\u计数\u值的出现次数
  • 获取具有最大出现次数的键
    array\u key
    max
注意事项

如果两个值相等,则它们都将在结果中返回。比如说

$string = 'ababababcd';
输出

 1 //or e in the commented one I tested
Array
(
   [0] => a
   [1] => b
)
你从来没有提到你想在这种情况下发生什么(平局)。对于您的特殊情况,这甚至可能不是一个问题。但为了完整起见,我不得不提一下。如果不希望以数组形式返回,可以执行以下操作(返回false或第一个元素):

干杯