```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 ``` ```= \$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; \$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; } \$set = explode(' ', 'a b c'); // \$all will contain the final output \$all = \$set; while(count(\$set) > 1) { \$perms = array(); \$size = count(\$set) - 1; \$perm = range(0, \$size); \$j = 0; do { foreach (\$perm as \$i) { \$perms[\$j][] = \$set[\$i]; } } while (\$perm = pc_next_permutation(\$perm, \$size) and ++\$j); foreach (\$perms as \$p) { \$all[] = implode(' ', \$p); } array_pop(\$set); } // display results foreach(\$all as \$each) { echo \$each . "\n"; } ```
 ```1 2 3 4 5 6 7 8 9 10 11 ``` ```a b c a b c a c b b a c b c a c a b c b a a b b a ```