具有优先级的PHP置换数组
这里我有一个代码,它提供了我所需的输出,但我想在选择它时添加优先级顺序 所有可能的组合码具有优先级的PHP置换数组,php,arrays,sorting,Php,Arrays,Sorting,这里我有一个代码,它提供了我所需的输出,但我想在选择它时添加优先级顺序 所有可能的组合码 $array = array('x2', 'y', 'm'); function depth_picker($arr, $temp_string, &$collect) { if ($temp_string != "") $collect []= $temp_string; for ($i=0; $i<sizeof($arr);$i++) {
$array = array('x2', 'y', 'm');
function depth_picker($arr, $temp_string, &$collect) {
if ($temp_string != "")
$collect []= $temp_string;
for ($i=0; $i<sizeof($arr);$i++) {
$arrcopy = $arr;
$elem = array_splice($arrcopy, $i, 1); // removes and returns the ith element
if (sizeof($arrcopy) > 0) {
depth_picker($arrcopy, $temp_string ." " . $elem[0], $collect);
} else {
$collect []= $temp_string. " " . $elem[0];
}
}
}
$collect = array();
depth_picker($array, "", $collect);
print_r($collect);
同样地
function array_permutation(array $array)
{
$count = array_map('count', $array);
$finalSize = array_product($count);
$arraySize = count($array);
$output = array_fill(0, $finalSize, []);
$i = 0;
$c = 0;
for (; $i < $finalSize; $i++) {
for ($c = 0; $c < $arraySize; $c++) {
print $output[$i][] = $array[$c][$i % $count[$c]];
}
print "<br/>";
}
return $output;
}
$array = [['x2','x1'], ['y','k'], ['m', 'n']];
//print $output;
$output= array_permutation($array);
x2
x1
y
k
m
n
x2y
x1y
x2k
x1k
x2ym
x1ym
x2yn
x1yn
x2km
x1km
x2kn
x1kn