繁体   English   中英

PHP 计算 20 个单词中出现的字符并进行比较以计算/查找关键字

[英]PHP count characters occurrence in 20 words and compare to calculate / find Keyword

我需要在以下条件下从 20 个单词的列表中查找/计算 1 个单词:

  1. 如果数组中最长的单词为 5 个字符,则结果将为 5 个字符。 (我要查找的单词总是 4、5 或 6 个字符长。)
  2. 计算/识别每个字符及其在 word 中的位置以供以后计算。
  3. 数组中的单词包含大写字母 AZ 和/或数字 0-9。

这是我的20个字:

$words = array( 'MVW',
                'MWAH',
                'MWAH',
                'MYW',
                'MW',
                'MY9AH',
                'MYQAH',
                'MYQAH',
                'MY9AH',
                'MYQAH',
                'MYQAH',
                'MWAH',
                'MYQAH',
                'MYSWI',
                'MYQAH',
                'MYQAH',
                'MW',
                'MW',
                'MW',
                'MW');

我需要计算行中的字符数并找到出现次数最多的字符以获得此结果:

    1. char is: M - occurred 20 times as 1. character in words.
    2. char is: Y - 11 times.
    3. char is: Q - 7 times.
    4. char is: A - 10 times.
    5. char is: H - 9 times.    

我想要的数组 $words 的结果是: MYQAH

我试过这个代码:

<?php

$words = array( 'MVW',
                'MWAH',
                'MWAH',
                'MYW',
                'MW',
                'MY9AH',
                'MYQAH',
                'MYQAH',
                'MY9AH',
                'MYQAH',
                'MYQAH',
                'MWAH',
                'MYQAH',
                'MYSWI',
                'MYQAH',
                'MYQAH',
                'MW',
                'MW',
                'MW',
                'MW');


$newarray = array();
$cc2 = 0;
$cc3 = 0;
$cc4 = 0;
$cc5 = 0;
$cc6 = 0;

foreach($words as $run) {

    if (isset($run['1']) && !isset($run['2'])) {
        $newarray[] = array($run['0'],$run['1']);
        $cc2++;
    }

    if (isset($run['2']) && !isset($run['3'])) {
        $newarray[] = array($run['0'],$run['1'],$run['2']);
        $cc3++;
    }

    if (isset($run['3']) && !isset($run['4'])) {
        $newarray[] = array($run['0'],$run['1'],$run['2'],$run['3']);
        $cc4++;
    }

    if (isset($run['4']) && !isset($run['5'])) {
        $newarray[] = array($run['0'],$run['1'],$run['2'],$run['3'],$run['4']);
        $cc5++;
    }

    if (isset($run['5']) && !isset($run['6'])) {
        $newarray[] = array($run['0'],$run['1'],$run['2'],$run['3'],$run['4'],$run['5']);
        $cc6++;
    }

}

echo "Length / Found words<br>\n";
echo "2 chars / $cc2<br>\n";
echo "3 chars / $cc3<br>\n";
echo "4 chars / $cc4<br>\n";
echo "5 chars / $cc5<br>\n";
echo "6 chars / $cc6<br>\n";

echo "<pre>";
var_dump($newarray);
echo "</pre>";
?>

我得到了这个结果:

Length / Found words
2 chars / 5
3 chars / 2
4 chars / 3
5 chars / 10
6 chars / 0
array(20) {
  [0]=>
  array(3) {
    [0]=>
    string(1) "M"
    [1]=>
    string(1) "V"
    [2]=>
    string(1) "W"
  }
  [1]=>
  array(4) {
    [0]=>
    string(1) "M"
    [1]=>
    string(1) "W"
    [2]=>
    string(1) "A"
    [3]=>
    string(1) "H"
  }
  [2]=>
  array(4) {
    [0]=>
    string(1) "M"
    [1]=>
    string(1) "W"
    [2]=>
    string(1) "A"
    [3]=>
    string(1) "H"
  }
  [3]=>
  array(3) {
    [0]=>
    string(1) "M"
    [1]=>
    string(1) "Y"
    [2]=>
    string(1) "W"
  }
  [4]=>
  array(2) {
    [0]=>
    string(1) "M"
    [1]=>
    string(1) "W"
  }
  [5]=>
  array(5) {
    [0]=>
    string(1) "M"
    [1]=>
    string(1) "Y"
    [2]=>
    string(1) "9"
    [3]=>
    string(1) "A"
    [4]=>
    string(1) "H"
  }
  [6]=>
  array(5) {
    [0]=>
    string(1) "M"
    .........

问题:从数组中的上述单词中获得结果的最佳方法是什么: MYQAH

非常感谢你的帮助。

我有一个聪明的小单线给你!

代码:(演示

echo implode(
        array_map(function(){
                $occurrences=count_chars(implode(func_get_args()),1);
                arsort($occurrences);
                return chr(key($occurrences));
            },
            ...array_map('str_split',$words)
        )
    );

输出:

MYQAH

故障:(我不会用所有的输出来膨胀这个页面,去这个Demo

$words=['MVW','MWAH','MWAH','MYW','MW','MY9AH','MYQAH','MYQAH','MY9AH','MYQAH',
        'MYQAH','MWAH','MYQAH','MYSWI','MYQAH','MYQAH','MW','MW','MW','MW'];

echo "*** Step #1:  Replace each word with an array of its characters ***\n";
var_export(array_map('str_split',$words));        
echo "\n\n---\n\n";

echo "*** Step #2:  Pass the characters through array_map with the splat operator and func_get_args() to isolate columnar data including NULLs where no character exists in the column ***\n";
var_export(array_map(function(){return func_get_args();},...array_map('str_split',$words)));
echo "\n\n---\n\n";

echo "*** Step #3:  Convert column data to strings with the added benefit of eliminating NULLs ***\n";
//var_export(array_map(function(){return implode(func_get_args());},...array_map('str_split',$words)));
echo "\n\n---\n\n";

echo "*** Step #4:  Count the occurrences of each character; stored as ord values as keys, and occurrences as values ***\n";
var_export(array_map(function(){return count_chars(implode(func_get_args()),1);},...array_map('str_split',$words)));
echo "\n\n---\n\n";

echo "*** Step #5:  Sort DESC while preserving keys ***\n";
var_export(array_map(function(){$occurrences=count_chars(implode(func_get_args()),1); arsort($occurrences); return $occurrences;},...array_map('str_split',$words)));
echo "\n\n---\n\n";

echo "*** Step #6:  Target the first (highest occurring) value/character in the array ***\n";
var_export(array_map(function(){$occurrences=count_chars(implode(func_get_args()),1); arsort($occurrences); return key($occurrences);},...array_map('str_split',$words)));
echo "\n\n---\n\n";

echo "*** Step #7:  Convert the targeted character from ord() to chr() ***\n";
var_export(array_map(function(){$occurrences=count_chars(implode(func_get_args()),1); arsort($occurrences); return chr(key($occurrences));},...array_map('str_split',$words)));

ps要隔离旋转字母数组“90度”的“花哨技巧”,请查看我使用相同方法的另一篇文章


这是使用循环构造的相同通用方法,它对早期的 PHP 版本更加宽容:

代码:(演示

$words=['MVW','MWAH','MWAH','MYW','MW','MY9AH','MYQAH','MYQAH','MY9AH','MYQAH',
        'MYQAH','MWAH','MYQAH','MYSWI','MYQAH','MYQAH','MW','MW','MW','MW'];

$chars=array_map('str_split',$words);
usort($chars,function($a,$b){return sizeof($b)-sizeof($a);});

$result='';
foreach($chars[0] as $col=>$not_used){
    $occurrences=array_count_values(array_column($chars,$col));  // no NULL values
    arsort($occurrences);
    $result.=key($occurrences);
}
echo $result;  // same output: MYQAH

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM