function pc_permute($items, $perms = array( )) {
if (empty($items)) {
print join(' ', $perms) . "\n";
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
source: http://docstore.mik.ua/orelly/webprog/pcook/ch04_26.htm#phpckbk-CHP-4-EX-6
also check next example on that site
example: pc_permute( explode( ' ', 'buy new microsoft windows' ) );
buy new microsoft windows
new buy microsoft windows
buy microsoft new windows
microsoft buy new windows
new microsoft buy windows
microsoft new buy windows
buy new windows microsoft
new buy windows microsoft
buy windows new microsoft
windows buy new microsoft
new windows buy microsoft
windows new buy microsoft
buy microsoft windows new
microsoft buy windows new
buy windows microsoft new
windows buy microsoft new
microsoft windows buy new
windows microsoft buy new
new microsoft windows buy
microsoft new windows buy
new windows microsoft buy
windows new microsoft buy
microsoft windows new buy
windows microsoft new buy
windows new microsoft
be valid? – Schapirawindows new windows
ornew new new
? – Alena