【问题标题】:Count number of occurences of words from one array in another array计算另一个数组中一个数组中单词的出现次数
【发布时间】:2015-08-06 15:08:49
【问题描述】:

我有两个数组:$story$languages

现在我想知道$languages 中的元素或值出现在$story 数组中的次数。这意味着$languages数组的“php”、“javascript”、“mysql”在$story中出现了多少次?

如何在 php 中做到这一点?任何人都可以帮忙吗?这是我的数组...

$story = array();
$story[] = 'As a developer, I want to refactor the PHP & javascript so that it has less duplication';
$story[] = ' As a developer, I want to configure Jenkins so that we have continuous integration and I love mysql';
$story[] = ' As a tester, I want the test cases defined so I can test the system using php language and phpunit framework';

$languages = array(
  'php', 'javascript', 'mysql'  
);

【问题讨论】:

标签: php


【解决方案1】:

我会将总数存储在您的 $languages 数组中。添加前无需检查计数,因为添加 0 不会影响您的总数。

<?
    $story = array();
    $story[] = 'As a developer, I want to refactor the PHP & javascript so that it has less duplication';
    $story[] = ' As a developer, I want to configure Jenkins so that we have continuous integration and I love mysql';
    $story[] = ' As a tester, I want the test cases defined so I can test the system using php language and phpunit framework';

    $languages = array(
        'php' => 0,
        'javascript' => 0,
        'mysql' => 0
    );

    foreach ($story as $sk => $sv){
        foreach ($languages as $lk => $lv){
            $languages[$lk] += substr_count($story[$sk], $lk);
        }
    }

    print_r($languages);
?>

【讨论】:

  • 好答案。如果需要单词边界或无大小写匹配,可以将substr_count 部分替换为$languages[$lk] += preg_match_all('/\b'.preg_quote($lk, "/").'\b/i', $story[$sk]);
  • 嗯,但不重叠,正则表达式会解决这个问题吗?我真的想相信 OP 不会有任何重叠。拜托,请不要打架......
  • OP没有提到重叠,也不是无大小写匹配或边界,只是想作为补充提及:]
  • 是的,我希望任何合理的“故事”都有混合大小写和边界问题。重叠意味着这个故事一开始就很糟糕。
【解决方案2】:

有 2 个循环: https://ideone.com/grRBeG

$story = array();
$story[] = 'As a developer, I want to refactor the PHP & javascript so that it has less duplication';
$story[] = ' As a developer, I want to configure Jenkins so that we have continuous integration and I love mysql';
$story[] = ' As a tester, I want the test cases defined so I can test the system using php language and phpunit framework';

$languages = [
    'php' => 0,
    'javascript' => 0,
    'mysql' => 0,
];

for( $i=0,$size=count($story); $i<$size; ++$i )
{
    $text = strtolower($story[$i]);
    foreach( $languages as $word => $nb )
        $languages[$word] += substr_count($text, $word);
}

var_export($languages);

【讨论】:

  • 由于这主要是我的答案的副本,我应该重申 cmets 所说的话,substr_count 负责大小写匹配。所以你知道,在这种情况下运行strtolower 只会增加处理并且没有任何作用。
  • 我不会给你投反对票,不值得,保留你的 10 分,但其他来到这个页面的人应该知道要避免这个答案,不管它有多俗气。
【解决方案3】:

运行两个foreach 循环,如下所示。

$finalCount = array();
foreach($story as $current)
{
      $count=0;
     foreach($languages as $language){
         if (substr_count($current,$language) !== false) {
           $count++
         }
     }
     $finalCount[$language] = $count;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多