我构建了两个版本的PHP 7函数,它接受一个数组,并返回一个数组列表,显示原始数组成员的所有排列。例如,对于输入[1,2,3]
,预期输出将是所有六个排列的1,2和3。
我希望这两个版本的函数能够提供相同的输出但却无法弄清楚它们为什么没有。这是第一个(按预期工作):
function permutations(array $input): array {
$func = function (array $selected, array $chooseFrom, array &$results)
use (&$func) {
foreach ($chooseFrom as $k => $unchosen):
$selectedCopy = $selected; // make a copy
$chooseFromCopy = $chooseFrom; // make a copy
$selectedCopy[] = $unchosen; // add the next unchosen item to selected list
array_splice($chooseFromCopy, $k,1); // remove the item from chooseFrom list
$func($selectedCopy, $chooseFromCopy, $results); // recursive call
endforeach;
// If we've used all items. Add selection to results
if (empty($chooseFrom)) $results[] = $selected;
};
$results = [];
$func([], $input, $results);
return $results;
}
当我致电permutations([1,2])
时,我得到了预期的结果:[[1,2],[2,1]]
。
这是该函数的非工作版本。唯一的区别在于foreach
:
function permutations2(array $input): array {
$func = function (array $selected, array $chooseFrom, array &$results)
use (&$func) {
foreach ($chooseFrom as $k => $unchosen):
$chooseFromCopy = $chooseFrom; // make a copy
$selected[] = $unchosen; // add the next unchosen to the selected list
array_splice($chooseFromCopy, $k, 1); // remove the item from chooseFrom list
$func($selected, $chooseFromCopy, $results); // recursive call
endforeach;
// If we've used all items. Add selection to results
if (empty($chooseFrom)) $results[] = $selected;
};
$results = [];
$func([], $input, $results);
return $results;
}
当我致电permutations2([1,2])
时,我收到了错误的结果:[[1,2],[1,2,1]]
为什么会有差异?
答案 0 :(得分:2)
问题是关于变量“$ selected”保存for循环的第一次迭代的结果,并且需要在进入循环的下一次迭代之前重新初始化。在for循环之前将“$ selected”存储在另一个变量(比如说$ tempselected)中,并在endforeach语句之前使用$ tempselected重新初始化“$ selected”变量将使代码工作。但是这种变化几乎与函数的工作样本相同:)
<?php
function permutations2(array $input): array {
$func = function (array $selected, array $chooseFrom, array &$results)
use (&$func) {
$selectedTemp = $selected;
foreach ($chooseFrom as $k => $unchosen):
$chooseFromCopy = $chooseFrom; // make a copy
$selected[] = $unchosen; // add the next unchosen to the selected list
array_splice($chooseFromCopy, $k, 1); // remove the item from chooseFrom list
$func($selected, $chooseFromCopy, $results); // recursive call
$selected = $selectedTemp;
endforeach;
echo("<br>After For Loop <br>");
// If we've used all items. Add selection to results
if (empty($chooseFrom)) { $results[] = $selected; }
};
$results = [];
$func([], $input, $results);
return $results;
}
$res = permutations2(['a','b','c']);