原文链接 http://blog.shuxuemi.cn/web/index/detail/16
// 全排
function fullArrange(&$arr, $index)
{
if ($index == 0) {
return [$arr[0]];
} else {
$rows = fullArrange($arr, $index - 1);
$temp_len = count($rows);
$temp_arr = [];
for ($k = 0; $k < $temp_len; $k++) {
$row = $rows[$k];
for ($j = 0; $j <= strlen($row); $j++) {
$str = substr($row, 0, $j) . $arr[$index] . substr($row, $j);
$temp_arr[] = $str;
}
}
return $temp_arr;
}
}
// 全组合
function fullCombine($arr, $index)
{
if ($index == 0) {
return [$arr[0]];
} else {
$rows = fullCombine($arr, $index - 1);
$temp_len = count($rows);
$rows[] = $arr[$index];
for ($k = 0; $k < $temp_len; $k++) {
$rows[] = $rows[$k] . $arr[$index];
}
return $rows;
}
}
//$arr = "abc";
//$arranges = fullArrange($arr, 2);
//$combines = fullCombine($arr, 2);
//print_r($arranges);
//print_r($combines);