function print_words_permutations($items, $perms = array()) {
if (empty($items)) {
echo implode(' ', $perms) . "\n";
} else {
for ($i = 0; $i < count($items); $i++) {
$newItems = $items;
$newPerms = $perms;
list($foo) = array_splice($newItems, $i, 1);
array_unshift($newPerms, $foo);
print_words_permutations($newItems, $newPerms);
}
}
}
$words = ["word-1", "word-2", "word-3"];
print_words_permutations($words);
/*
run:
word-3 word-2 word-1
word-2 word-3 word-1
word-3 word-1 word-2
word-1 word-3 word-2
word-2 word-1 word-3
word-1 word-2 word-3
*/