首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >数组php中出现最多的字符?

数组php中出现最多的字符?
EN

Stack Overflow用户
提问于 2018-04-01 18:12:19
回答 2查看 195关注 0票数 -3

请帮助我创建一个函数来解析以下数组,并返回一个包含最常出现的字符大小写不敏感且排除特殊字符的数组,计数输入

    $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);
}

请帮我做一个用于上述目的的函数

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-01 18:29:03

您可以使用array_map()在每个项目上应用一个函数。在此函数中,为了使用array_count_values(),您可以将字符串转换为小写,在数组中拆分字符(使用array_filter()删除空格)。然后,您可以使用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));
});
票数 3
EN

Stack Overflow用户

发布于 2018-04-01 18:49:38

我会使用一次匹配一个字母或数字的模式的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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49596989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档