请帮我创建一个函数来解析下面的数组,并返回一个包含最常出现的字符不区分大小写的数组,并排除特殊字符,计数输入
$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);
}
请帮我为上述目的制作一个功能
答案 0 :(得分:3)
您可以使用array_map()
对每个项目应用功能。在此函数中,您可以将字符串转换为小写,将数组拆分为数组(并array_filter()
以删除空格),以便使用array_count_values()
。然后,您可以使用arsort()
对数组进行排序,以保持键关联并在顶部获取最常用的字符。最后,您可以使用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));
});
答案 1 :(得分:1)
我使用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);
答案 2 :(得分:0)
$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()到$ count来记录事件,并使用max()获取最大值。如果出现次数小于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));
DEMO:http://sandbox.onlinephpfunctions.com/code/e2fb793031cd172507d8a464b1096b1b2bb73046