Php 数组中最常出现的字符?
请帮助我创建一个函数来解析以下数组,并返回一个数组,该数组包含最常见的字符,不区分大小写,不包括特殊字符,计数输入Php 数组中最常出现的字符?,php,arrays,character,counting,Php,Arrays,Character,Counting,请帮助我创建一个函数来解析以下数组,并返回一个数组,该数组包含最常见的字符,不区分大小写,不包括特殊字符,计数输入 $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 animal
$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);
}
请帮助我为上述目的创建一个函数您可以使用该函数对每个项目应用函数。在该函数中,您可以将字符串转换为小写,拆分数组中的字符并删除空格,以便使用。然后,您可以使用对数组进行排序以保持键关联,并在顶部获得使用最多的字符。最后,您可以使用和重置来获取数组的第一个键和第一个值:
$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和一次匹配一个字母或数字的模式来提取一个字符数组,然后使用数组计数值查找出现的次数,按出现的次数降序排列数组,然后提取第一个键和第一个值,它们表示出现的最高字母的字符和计数
代码:演示:
使用
我使用count_chars将出现次数绘制为$counts,并使用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>';
演示:您是否先尝试自己做?告诉我们你尝试了什么。不,这是为我的项目,然后,你已经尝试解决它,不是吗?你只是从php手册复制它。请发布您尝试过的内容。始终通过编辑而不是评论向您的问题添加问题详细信息。请在编辑您的问题后删除您的评论。是否删除特殊注释characters@sarinsurendran你说的特殊字符是什么意思?如果句子包含特殊字符,它不会被算作最常见的特殊字符@etc@sarinsurendran您应该使用除trim之外的另一个函数是array_filter函数。也许是一个自定义函数来删除它们。@sarin我的方法更干净/更直接。是否也要包括数字?请回答我的问题。你要电话号码吗?下划线?是的,数字和下划线避免删除特殊字符。那么我将调整我的答案以包括数字。沙林·苏伦德兰请为新问题创建新帖子
$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',
];
<?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));