Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:43:46+00:00 2026-06-15T09:43:46+00:00

I am having an issue with a recursive query. I am trying to create

  • 0

I am having an issue with a recursive query. I am trying to create a php script that will search a lexicon and return words that can be made with a given set of letters, like a scrabble word finder. I know there are other threads with great solutions, but I couldn’t find a solution to my issue there. As the function continues recursively, it is finding words with multiple duplicates of a letter. For example, if I initially call the function with the letters a, d, and t, the function will return the word ‘data’, which has two A’s. I thought the bottom segment of my code should prevent that by removing a used letter from the array, but I am missing something. I should note that I am a novice, currently in school, but this is not for a school project of any kind, I am just trying to learn something on my own. Thanks in advance.

    // $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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:43:47+00:00Added an answer on June 15, 2026 at 9:43 am

    -Solved-
    I solved the issue by initializing the $foundWords array once for each letter, and not once for the function. I think it was saving prior searches before it was removing the letter from the array at the bottom of the function call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having an issue here that I have tried everything I can think of but
I having issue that content assistant / intellisense is working in methods such as
Hi guys I am having issue I have this query: SELECT * FROM useraccount
Having an issue with random individuals trying to access an intranet site with a
Here's what I'm trying to accomplish with this program: a recursive method that checks
I've written a script that will be used for archiving log files from a
I am currently having a bit of an issue trying to parse a table
Having issue with my navigation. How can I prevent it? My CSS file looks
I'm having issue working with json_decode() in PHP. Im using the json2.js library to
I am having an issue trying to recursively use a generic function for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.