Hi guys,

I'm working on a little script that generates all combinations of a given input string. My problem is, it's generating the combinations perfectly fine (I checked with an echo), but not inserting them in to an array. Here is my code:

function check($width, $position, $base_string, $charSet)
{
    $combinations = array();    

    for ($i = 0; $i < strlen($charSet); $i++) 
    {
        if ($position  < $width - 1) 
            check($width, $position + 1, $base_string . $charSet[$i], $charSet);            

        $combinations[] = $base_string . $charSet[$i];
        //echo statement here for debugging
    }

    return $combinations;
}

$word = "hello"

$combinations = array_unique(check(strlen($word), 0, "", $word));

foreach ($combinations as $value)
    echo $value;

For some reason, if I add an echo statement to where I marked it in the comment, it prints the values fine. For some reason, it's not inserting them in to the array.

what you return you are not passing back to calling function

if ($position  < $width - 1) 
            check($width, $position + 1, $base_string . $charSet[$i], $charSet); 

You must store output of check(...)function to some array, so you are looing your processsed output here.

         if ($position  < $width - 1) 
            $retcomb=check($width, $position + 1, $base_string . $charSet[$i], $charSet); 

Check this

function check($width, $position, $base_string, $charSet,$combinations)
{
    for ($i = 0; $i < strlen($charSet); $i++) 
    {
        if ($position  < $width - 1) 
         $combinations .= check($width, $position + 1, $base_string.$charSet[$i], $charSet, $combinations);            

       $combinations .= $base_string . $charSet[$i].'-';
    }

        return $combinations;
}

$word = "Hel";

$combinations = check(strlen($word), 0, "", $word, '');
$values = array_unique(explode("-", $combinations));

foreach ($values as $value)
    echo $value.'<br />';