Php 递归函数有问题吗
我对递归查询有问题。我正在尝试创建一个php脚本,该脚本将搜索词典并返回可以用给定字母集生成的单词,就像拼字搜索器一样。我知道还有其他线程有很好的解决方案,但我在那里找不到解决我问题的方法。当函数递归继续时,它将查找具有多个字母副本的单词。例如,如果我最初用字母a、d和t调用函数,函数将返回单词“data”,它有两个a。我认为我的代码的底部部分应该通过从数组中删除一个用过的字母来防止这种情况,但是我遗漏了一些东西。我应该注意的是,我是一个新手,目前在学校,但这不是一个学校的任何项目,我只是试图学习自己的东西。提前谢谢Php 递归函数有问题吗,php,recursion,Php,Recursion,我对递归查询有问题。我正在尝试创建一个php脚本,该脚本将搜索词典并返回可以用给定字母集生成的单词,就像拼字搜索器一样。我知道还有其他线程有很好的解决方案,但我在那里找不到解决我问题的方法。当函数递归继续时,它将查找具有多个字母副本的单词。例如,如果我最初用字母a、d和t调用函数,函数将返回单词“data”,它有两个a。我认为我的代码的底部部分应该通过从数组中删除一个用过的字母来防止这种情况,但是我遗漏了一些东西。我应该注意的是,我是一个新手,目前在学校,但这不是一个学校的任何项目,我只是试图学
// $wordsArray is the array pf possible words so far
// $lettersArray is the array of remaining letters
// $position is the current search position of the words
// $solvedWords is an array containing all of the solved words
function search($wordsArray, $lettersArray, $position, $solvedWords) {
// if there are letters in the letters array continue
if (count($lettersArray) > 0) {
$foundWords = array();// foundWords is an array containing possible words given the current letter searched
// for each remaining letter, check each word for a match at the next position
foreach ($lettersArray AS $letter) {
foreach ($wordsArray AS $word) {
// if there is a match with the current letter at the current search position, check to see if it is a complete word
if (substr($word, $position, 1) == $letter) {
if (strlen($word) == ($position+1)) {
array_push($solvedWords, $word); // if complete, add to the solved words
} else {
array_push($foundWords, $word); // if not a complete word, add to the foundWords array of possible words
} // end if
} // end if
} // end foreach
// $remainingLetters array should be the same as the letters array but removed the current letter at this position
$remainingLetters = $lettersArray;
$done = false;
// if there is a letter match, remove the letter from the remainingLetters array
for ($i = 0; $i < count($remainingLetters); $i++) {
if (!$done) {
if ($letter == $remainingLetters [$i]) {
unset ($remainingLetters [$i]);
$remainingLetters = array_values($remainingLetters);
$done = true;
} // end if
} // end if
} // end foreach
if ($remainingLetters)
$solvedWords = search($foundWords, $remainingLetters, ($position+1), $solvedWords);
} // end foreach
return $solvedWords;
} // end if
} // end search()
/$wordsArray是到目前为止可能出现的单词的数组
//$lettersArray是剩余字母的数组
//$position是单词的当前搜索位置
//$solvedWords是包含所有已解单词的数组
函数搜索($wordsArray、$lettersArray、$position、$solvedWords){
//如果字母数组中有字母,请继续
如果(计数($lettersArray)>0){
$foundWords=array();//foundWords是一个数组,包含给定当前搜索字母的可能单词
//对于剩余的每个字母,在下一个位置检查每个单词是否匹配
foreach($lettersArray作为$letter){
foreach($wordsArray作为$word){
//如果在当前搜索位置与当前字母匹配,请检查是否为完整单词
if(substr($word,$position,1)==$letter){
如果(strlen($word)=($position+1)){
数组_push($solvedWords,$word);//如果完成,则添加到已解单词中
}否则{
array_push($foundWords,$word);//如果不是完整的单词,请添加到可能的单词的foundWords数组中
}//如果结束,则结束
}//如果结束,则结束
}//结束foreach
//$remaingletters数组应与字母数组相同,但已删除此位置的当前字母
$remainingleters=$lettersArray;
$done=false;
//如果存在字母匹配,请将该字母从remaingletters数组中删除
对于($i=0;$i
-已解决-
我解决了这个问题,为每个字母初始化$foundWords数组一次,而不是为函数初始化一次。我认为它在从函数调用底部的数组中删除字母之前保存了先前的搜索。在没有彻底阅读和测试代码的情况下,我认为您应该使用
foreach
在底部循环remainingler
,和/或使用array\u slice
制作不包含给定索引()的数组的副本,而不是unset
ing数组索引。如果您unset
数组索引,然后根据索引计数在其上循环,您可能最终尝试访问一个unset值。感谢您的建议taz,我将实现它。我还为每个字母初始化$foundWords数组一次,而不是为函数初始化一次,从而解决了这个问题。我认为它在从函数调用底部的数组中删除字母之前保存了先前的搜索。