2

i want use foreach loop dynamically in php, i have task to print AAA to 999 and i'm using this code and it's work perfect,

Reference :: http://www.onlinecode.org/generate-string-aaaa-9999-using-php-code/

$addalphabeth = array_merge(range('A', 'Z'), range(0,9));
$setcharacter = [];
foreach ($addalphabeth as $setcharacter[0]) {
  foreach ($addalphabeth as $setcharacter[1]) {
    foreach ($addalphabeth as $setcharacter[2]) {
        //$setcatalog[] = vsprintf('%s%s%s', $setcharacter);
        echo "<b>".vsprintf('%s%s%s', $setcharacter)."</b>";
        echo "<br>";
    }
  }
}

now my issue is have to print that AAAAA to 99999 (need loop 5 time) or AAAAAAAA to 99999999 (need loop 8 time) , so for this i use loop n time. I have no idea how to do it, any help or suggestion will be appreciated.

4
  • 1
    Any reason for the Downvote please? Commented Nov 13, 2017 at 5:14
  • 1
    From your code, it looks like you could combine a foreach and for loop with n iterations. Commented Nov 13, 2017 at 6:11
  • @fubar : thanks for suggestion Commented Nov 13, 2017 at 6:19
  • 2
    Recusion might work well too. Commented Nov 13, 2017 at 6:26

1 Answer 1

1

Evening, here is my solution, using recursion. To setup a recursion, you have to think of two things. What is your stop condition, and what do to with non-stopping conditions.

  • Stopping condition: when you ask for only 1 char width, simply loop through each member of $addalphabeth. And since that width == 1 scenario will happen when the final width you want is >1, add the prefix to it.
  • Non-stopping condition: lets start with width 2. You want the every first char to be followed by all possible second chars. So call the function with width = 1 and prefix = the first char.
  • for 3, you want the first char followed by every char in the second position, followed by every char in the third position.
  • and so on...
  • So the general case is: from left to right, with n chars width, loop on every char, and print every possible combination of n-1 chars after it.

So here is the code:

function printAto9($width,$prefix = '')
{
    $chars = array_merge(range('A', 'Z'), range(0,9));
    if ($width == 1)
    {
        foreach ($chars as $char)
        {
            echo "$prefix$char\n";
        }
    }
    else
    {
        $width--;
        foreach ($chars as $char)
        {
            echo printAto9($width,$prefix . $char);
        }
    }
}

printAto9(5);

I put printAto9(5); as a test, you can use whatever. But careful, the output is monstrous pretty quick, and you can kill your php (memory or time limit).

Sign up to request clarification or add additional context in comments.

2 Comments

i have find solution, but really good code thanks for answer.
@JON Can u post your solution as an another answer ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.