-2
我是一个小白试图解决我正在做的单词搜索应用程序。我的目标是获取一个字符串,并比较该字符串的每个字母出现在另一个字符串中的次数。然后将该信息放入一个键值对的数组中,其中键是第一个字符串的每个字母,值是次数。然后用ar(排序)排序并最终回显出现最多的字母的值(因此具有最高值的键)。如何创建并填充一个新的关联数组,每个数组都创建一个值?
所以它会像array('t'=> 4,'k'=>'9','n'=> 55),回显'n'的值。谢谢。
这是我到目前为止是不完整的。
<?php
$i = array();
$testString= "endlessstringofletters";
$testStringArray = str_split($testString);
$longerTestString= "alphabetalphabbebeetalp
habetalphabetbealphhabeabetalphabetalphabetalphbebe
abetalphabetalphabetbetabetalphabebetalphabetalphab
etalphtalptalphabetalphabetalbephabetalphabetbetetalphabet";
foreach ($testStringArray AS $test) {
$value = substr_count($longerTestString, $testStringArray);
/* Instead of the results of this echo, I want each $value to be matched with each member of the $testStringArray and stored in an array. */
echo $test. $value;
}
/* I tried something like this outside of the foreach and it didn't work as intended */
$i = array_combine($testStringArray , $value);
print_r($i);
我不能完全肯定,如果这是一个确切的重复,但是这让我想起了很多[这个最近的问题](https://stackoverflow.com/questions/46733941/sorting-characters-by-count-using-php-or-python) –
什么是$字母?我不能看到它在任何地方声明 –
https://www.w3schools.com/php/php_arrays.asp会向你展示数组语法,看看关联数组。这将允许您替换该回声语句。 – Nic3500