数组中出现次数最多的字符 php?
most occuring characters in an array php?
请帮我做一个函数来解析以下数组和return一个包含最常出现的字符的数组,不区分大小写并排除特殊字符,计算输入
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
The code should output the following result:
Array
(
[0] => Array
(
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array
(
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array
(
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array
(
[sentence] => The tiger does have a coat of orange with black stripes
[character] => e
[occurrences] => 6
)
[4] => Array
(
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array
(
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
我试过了
foreach($sentences as $sentence) {
$value = array_count_values($sentence);
}
请帮我做一个功能达到上述目的
您可以使用 array_map()
to apply a function on each items. In this function, you could convert the string to lower case, split characters in an array (and array_filter()
to remove spaces), in order to use array_count_values()
. Then, you could sort the array using arsort()
to keep keys association and get the most used character in the top. Finally, you could use array_keys()
和 reset()
来获取数组的第一个键和第一个值:
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
$out = array_map(function($value) {
$chars = array_filter(str_split(strtolower($value)),'trim');
$vals = array_count_values($chars);
arsort($vals);
$keys = array_keys($vals);
return [
'sentence' => $value,
'character' => reset($keys),
'occurrences' => reset($vals),
];
}, $sentences);
print_r($out) ;
输出:
Array (
[0] => Array (
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array (
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array (
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array (
[sentence] => The tiger does have a coat of orange with black stripes
[character] => e
[occurrences] => 6
)
[4] => Array (
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array (
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
要删除特殊字符:
$chars = array_filter(str_split(strtolower($value)),function($val){
return trim(preg_replace('~\W+~', '', $val));
});
我会使用 preg_match_all()
和一次匹配一个字母或数字的模式来提取字符数组,然后使用 array_count_values()
查找出现的次数,对数组进行排序按降序出现,然后提取第一个键和第一个值(表示出现次数最多的字母的字符和计数)。
代码:(演示:https://3v4l.org/7OZ5d)
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
foreach ($sentences as $sentence) {
$alphanums = preg_match_all ('~[a-z\d]~', strtolower($sentence), $out) ? $out[0] : [];
// or: $alphanums = preg_split('~[^a-z\d]*~', strtolower($sentence), null, PREG_SPLIT_NO_EMPTY);
$occurrences = array_count_values($alphanums);
arsort($occurrences);
$result[] = [
"sentence" => $sentence,
"character" => key($occurrences),
"occurrences" => current($occurrences)
];
}
var_export($result);
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with **a** black stripe',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
我通过使用 count_chars() 到 $counts 来捕获出现次数,并通过使用 max() 获得最大出现次数。如果出现次数小于最大值,则从数组中删除 ASCII 代码(因为可能有多个字符具有相同的出现次数)。
通过调用 chr()
将剩余的 ASII 代码转换回字符
<?php
foreach ($sentences as $sentence){
$string = strtolower(preg_replace('/\s+/', '', $sentence));
$counts = count_chars($string, 1);
$max = max($counts);
foreach ($counts as $key => $count) {
if ($count < $max) {
unset($counts[$key]);
}
}
$characters = array_map(function($item){
return chr($item);
}, array_keys($counts));
$result[] = [
'sentence' => $sentence,
'character' => implode(',', $characters),
'occurrences' => $max
];
}
echo '<pre>', print_r($result) ,'<pre>';
输出:
Array
(
[0] => Array
(
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array
(
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array
(
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array
(
[sentence] => The tiger does have a coat of orange with a black stripe
[character] => a,e **<== multiple characters**
[occurrences] => 6
)
[4] => Array
(
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array
(
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
只计算字母:
$string = strtolower(preg_replace('/[^A-Za-z]/', '', $sentence));
演示:http://sandbox.onlinephpfunctions.com/code/e2fb793031cd172507d8a464b1096b1b2bb73046
请帮我做一个函数来解析以下数组和return一个包含最常出现的字符的数组,不区分大小写并排除特殊字符,计算输入
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
The code should output the following result:
Array
(
[0] => Array
(
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array
(
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array
(
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array
(
[sentence] => The tiger does have a coat of orange with black stripes
[character] => e
[occurrences] => 6
)
[4] => Array
(
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array
(
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
我试过了
foreach($sentences as $sentence) {
$value = array_count_values($sentence);
}
请帮我做一个功能达到上述目的
您可以使用 array_map()
to apply a function on each items. In this function, you could convert the string to lower case, split characters in an array (and array_filter()
to remove spaces), in order to use array_count_values()
. Then, you could sort the array using arsort()
to keep keys association and get the most used character in the top. Finally, you could use array_keys()
和 reset()
来获取数组的第一个键和第一个值:
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
$out = array_map(function($value) {
$chars = array_filter(str_split(strtolower($value)),'trim');
$vals = array_count_values($chars);
arsort($vals);
$keys = array_keys($vals);
return [
'sentence' => $value,
'character' => reset($keys),
'occurrences' => reset($vals),
];
}, $sentences);
print_r($out) ;
输出:
Array (
[0] => Array (
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array (
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array (
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array (
[sentence] => The tiger does have a coat of orange with black stripes
[character] => e
[occurrences] => 6
)
[4] => Array (
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array (
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
要删除特殊字符:
$chars = array_filter(str_split(strtolower($value)),function($val){
return trim(preg_replace('~\W+~', '', $val));
});
我会使用 preg_match_all()
和一次匹配一个字母或数字的模式来提取字符数组,然后使用 array_count_values()
查找出现的次数,对数组进行排序按降序出现,然后提取第一个键和第一个值(表示出现次数最多的字母的字符和计数)。
代码:(演示:https://3v4l.org/7OZ5d)
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with black stripes',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
foreach ($sentences as $sentence) {
$alphanums = preg_match_all ('~[a-z\d]~', strtolower($sentence), $out) ? $out[0] : [];
// or: $alphanums = preg_split('~[^a-z\d]*~', strtolower($sentence), null, PREG_SPLIT_NO_EMPTY);
$occurrences = array_count_values($alphanums);
arsort($occurrences);
$result[] = [
"sentence" => $sentence,
"character" => key($occurrences),
"occurrences" => current($occurrences)
];
}
var_export($result);
$sentences = [
0 => 'The tiger is the national animal of India',
1 => 'The tiger is a large carnivorous mammal that roams the forests',
2 => 'The tiger feeds on animals that also live in the forest',
3 => 'The tiger does have a coat of orange with **a** black stripe',
4 => 'Tigers, regardless of their subspecies, are carnivorous animals',
5 => 'The tiger is a protected species',
];
我通过使用 count_chars() 到 $counts 来捕获出现次数,并通过使用 max() 获得最大出现次数。如果出现次数小于最大值,则从数组中删除 ASCII 代码(因为可能有多个字符具有相同的出现次数)。
通过调用 chr()
将剩余的 ASII 代码转换回字符<?php
foreach ($sentences as $sentence){
$string = strtolower(preg_replace('/\s+/', '', $sentence));
$counts = count_chars($string, 1);
$max = max($counts);
foreach ($counts as $key => $count) {
if ($count < $max) {
unset($counts[$key]);
}
}
$characters = array_map(function($item){
return chr($item);
}, array_keys($counts));
$result[] = [
'sentence' => $sentence,
'character' => implode(',', $characters),
'occurrences' => $max
];
}
echo '<pre>', print_r($result) ,'<pre>';
输出:
Array
(
[0] => Array
(
[sentence] => The tiger is the national animal of India
[character] => i
[occurrences] => 6
)
[1] => Array
(
[sentence] => The tiger is a large carnivorous mammal that roams the forests
[character] => a
[occurrences] => 7
)
[2] => Array
(
[sentence] => The tiger feeds on animals that also live in the forest
[character] => e
[occurrences] => 7
)
[3] => Array
(
[sentence] => The tiger does have a coat of orange with a black stripe
[character] => a,e **<== multiple characters**
[occurrences] => 6
)
[4] => Array
(
[sentence] => Tigers, regardless of their subspecies, are carnivorous animals
[character] => s
[occurrences] => 8
)
[5] => Array
(
[sentence] => The tiger is a protected species
[character] => e
[occurrences] => 6
)
)
只计算字母:
$string = strtolower(preg_replace('/[^A-Za-z]/', '', $sentence));
演示:http://sandbox.onlinephpfunctions.com/code/e2fb793031cd172507d8a464b1096b1b2bb73046