I'm trying to adapt this code:
function pc_permute($items, $perms = array( )) {
if (empty($items)) {
echo implode($perms); // yield (implode($perms));
} 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);
}
}
}
To a yield, as you see commented in the same line. But doing so returns no values.
I'm calling it with:
foreach (pc_permute($a) as $y) {
echo $y;
}
Where $a should be something like $a = [0,1,2];
With echo works and with yield does not. What am I doing wrong?