protected static function getNextPermutation($p, $size) 
	{	    
	    // slide down the array looking for where we're smaller than the next guy
	    for ($i = $size - 1; $i >= 0 && $p[$i] >= $p[$i+1]; --$i) { }
	
	    // if this doesn't occur, we've finished our permutations
	    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
	    if ($i == -1) { return false; }

	    // slide down the array looking for a bigger number than what we found before
	    for ($j = $size; $j >= 0 && $p[$j] <= $p[$i]; --$j) { }
	
	    // swap them
	    $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
	
	    // now reverse the elements in between by swapping the ends
	    for (++$i, $j = $size; $i < $j; ++$i, --$j) {
	         $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
	    }
	
	    return $p;
	}