PHP计数字符以20个单词的形式出现,并比较计算 /查找关键字



我需要从20个单词的列表中查找/计算1个单词:

  1. 如果数组中的最长单词是5个字符,则结果将为5个字符。(我要寻找的话总是4、5或6个字符。(
  2. 计数/识别每个字符及其在单词中的位置以进行以后的计算。
  3. 数组中的单词包含大写字母A-Z和/或数字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');

我需要连续计数字符,并找到char的最高事件才能获得此结果:

    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.    

我想要的数组$单词的结果是: 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

崩溃:(我不会用所有输出膨胀此页面,转到此演示(

$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 "nn---nn";
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 "nn---nn";
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 "nn---nn";
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 "nn---nn";
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 "nn---nn";
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 "nn---nn";
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)));

P.S。要隔离旋转字母阵列" 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

最新更新