Use one of the two permutation algorithms discussed below.
Discussion
The pc_permute () function shown in Example 1 is a PHP modification of the basic recursive function.
example 1
function pc_permute($items, $perms = array()) { if (empty($items)) { echo implode(' ', $perms) . "<br>"; } 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); } } } pc_permute(explode(' ', 'she sells seashells'));
This recursion, though elegant, is ineffective because it makes copies everywhere. Moreover, it is not so easy to modify this function so that it returns values instead of being printed, without being accumulated in a global variable.
example 2
function pc_next_permutation($p, $size) { // проходим массив сверху вниз в поисках числа, которое меньше следующего for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { } // если такого нет, прекращаем перестановки // массив перевернут: (1, 2, 3, 4) => (4, 3, 2, 1) if ($i == -1) { return false; } // проходим массив сверху вниз в поисках числа, // превосходящего найденное ранее for ($j = $size; $p[$j] <= $p[$i]; --$j) { } // переставляем их $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp; // теперь переворачиваем массив путем перестановки элементов, // начиная с конца for (++$i, $j = $size; $i < $j; ++$i, --$j) { $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp; } return $p; } $set = explode(' ', 'she sells seashells'); $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) { echo implode(' ', $p) . "<br>"; }
The idea of Dominus is that instead of manipulating the array itself, you can create permutations of integers. Then, in order to obtain a true permutation, we again assign the elements of the array to the numbers — the original idea. However, this technique has some drawbacks. For us, PHP programmers, more important are the frequent extraction, insertion, and combining of arrays, that is, what is central to Perl. Then the process of calculating permutations of integers goes through a series of steps that are performed for each permutation; and since he does not remember the previous permutations, he begins every time from the original permutation. Why does repeated execution work if we can help it?