使用带有数组的soundex的PHP类似单词
我正在创建一个findSpellings函数,它有两个参数$word和$allWords$allwords是一个数组,它的单词拼写错误,听起来可能与$word变量类似。我试图完成的是基于soundex函数打印出与$word相似的所有单词。我无法用文字打印出数组。我的功能如下。如有任何帮助,将不胜感激:使用带有数组的soundex的PHP类似单词,php,Php,我正在创建一个findSpellings函数,它有两个参数$word和$allWords$allwords是一个数组,它的单词拼写错误,听起来可能与$word变量类似。我试图完成的是基于soundex函数打印出与$word相似的所有单词。我无法用文字打印出数组。我的功能如下。如有任何帮助,将不胜感激: <?php $word = 'stupid'; $allwords = array( 'stupid', 'stu and pid', 'hello',
<?php
$word = 'stupid';
$allwords = array(
'stupid',
'stu and pid',
'hello',
'foobar',
'stpid',
'supid',
'stuuupid',
'sstuuupiiid',
);
function findSpellings($word, $allWords){
while(list($id, $str) = each($allwords)){
$soundex_code = soundex($str);
if (soundex($word) == $soundex_code){
//print '"' . $word . '" sounds like ' . $str;
return $word;
return $allwords;
}
else {
return false;
}
}
}
print_r(findSpellings($word, $allWords));
?>
您不能有2个返回,第一个返回将退出代码
你可以这样做:
if (soundex($word) == $soundex_code){
//print '"' . $word . '" sounds like ' . $str;
$array = array('word' => $word, 'allWords' => $allWords);
return $array;
}
然后从$array中检索值,如下所示:
$filledArray = findSpellings($word, $allWords);
echo "You typed".$filledArray['word'][0]."<br/>";
echo "Were you looking for one of the following words?<br/>";
foreach($filledArray['allWords'] as $value)
{
echo $value;
}
$filledaray=findSpellings($word,$allWords);
echo“您键入的”。$filledArray['word'][0]。“
”;
echo“您是在寻找下列词语之一吗?
”;
foreach($filledArray['allWords']作为$value)
{
echo美元价值;
}
Ok。我怎样才能返回与soundex值匹配的单词的完整数组呢?我不是为了得到输出而这样做的。由于函数有两个参数,我需要为output@vpd05141989您正在为输出传递2个参数,但它们包含在一个数组中
$filledArray = findSpellings($word, $allWords);
echo "You typed".$filledArray['word'][0]."<br/>";
echo "Were you looking for one of the following words?<br/>";
foreach($filledArray['allWords'] as $value)
{
echo $value;
}