Finding Combinations with PHP Arrays

November 1st::2006 I wrote this to find combinations for an advanced sudoku generator. The code is commented so it should make sense. It is also a good function if you are just getting into recursion to look at. In the future, I may redo this function to also do permutations. [Update 11/03: Made a small improvement to speed it up further]

<?php
    function getCombinations ( $values ) 
    {
        $subsets = array('');

        # remove first element of array
        # v1 holds removed number
        while( $v = array_shift($values)) 
        {
            # get new combinations from the remaining values and 
            # combine those subsets with the saved value ($v) which 
            # in turn creates new subsets
            foreach ( getCombinations($values) as $subset )
                $subsets[] = $v . $subset;
        }

        # these subsets go back into the 
        # foreach or the recursion is finished
        return $subsets;
    }

    $a = array(1, 2, 3, 4);
?>

<pre><? print_r(getCombinations($a)); ?></pre>