Я хочу вычислить перестановки 5p2, 5p3, 5p4 и 5p5 из массива [1,2,3,4,5] Функция ниже работает только 5p5. Для запуска 5p2, 5p3, 5p4 мне придется вручную перебирать массив, используя for ... l oop. Пожалуйста, помогите мне.
//function to return permutations 5p5 array
function combinationArray($myarr) {
$results = [];
$current = [];
$next = function($myarr) use(&$next,&$results,&$current) {
$l = count($myarr);
if($l === 1) {
$current []= $myarr[0];
$results []= intval(implode($current)); //use this for array containing numbers only
//$results []= implode($current); //use this for array containing alphabets
array_pop($current);
return;
}
for($i=0; $i<$l; $i++) {
$tmpArr = $myarr;
$current []= array_splice($tmpArr,$i,1)[0];
$next($tmpArr);
array_pop($current);
}
};
$next($myarr);
return $results;
}
// Ручная итерация 5c2 будет такой ... примерно 120 различных модулей .....
$wordArray = str_split($sWord, 1);
$newArray1 = $wordArray[0]; $newArray2 = $wordArray[1];$newArray3 = $wordArray[2]; $newArray4 = $wordArray[3]; $newArray5 = $wordArray[4];
//12
for($a = 0; $a < count($newArray1); $a++){
for($b = 0; $b < count($newArray2); $b++){
$aux = $newArray1[$a].$newArray2[$b]; array_push($result52, $aux);
}
}
//13
for($a = 0; $a < count($newArray1); $a++){
for($c = 0; $c < count($newArray3); $c++){
$aux = $newArray1[$a].$newArray3[$c]; array_push($result52, $aux);
}
}
//14
for($a = 0; $a < count($newArray1); $a++){
for($d = 0; $d < count($newArray4); $d++){
$aux = $newArray1[$a].$newArray4[$d]; array_push($result52, $aux);
}
}
//15
for($a = 0; $a < count($newArray1); $a++){
for($e = 0; $e < count($newArray5); $e++){
$aux = $newArray1[$a].$newArray5[$e]; array_push($result52, $aux);
}
}
//21
for($b = 0; $b < count($newArray2); $b++){
for($a = 0; $a < count($newArray1); $a++){
$aux = $newArray2[$b].$newArray1[$a]; array_push($result52, $aux);
}
}
//23
for($b = 0; $b < count($newArray2); $b++){
for($c = 0; $c < count($newArray3); $c++){
$aux = $newArray2[$b].$newArray3[$c]; array_push($result52, $aux);
}
}
//24
for($b = 0; $b < count($newArray2); $b++){
for($d = 0; $d < count($newArray4); $d++){
$aux = $newArray2[$b].$newArray4[$d]; array_push($result52, $aux);
}
}
//25
for($b = 0; $b < count($newArray2); $b++){
for($e = 0; $e < count($newArray5); $e++){
$aux = $newArray2[$b].$newArray5[$e]; array_push($result52, $aux);
}
}
//31
for($c = 0; $c < count($newArray3); $c++){
for($a = 0; $a < count($newArray1); $a++){
$aux = $newArray3[$c].$newArray1[$a]; array_push($result52, $aux);
}
}
//32
for($c = 0; $c < count($newArray3); $c++){
for($b = 0; $b < count($newArray2); $b++){
$aux = $newArray3[$c].$newArray2[$b]; array_push($result52, $aux);
}
}
//34
for($c = 0; $c < count($newArray3); $c++){
for($d = 0; $d < count($newArray4); $d++){
$aux = $newArray3[$c].$newArray4[$d]; array_push($result52, $aux);
}
}
//35
for($c = 0; $c < count($newArray3); $c++){
for($e = 0; $e < count($newArray5); $e++){
$aux = $newArray3[$c].$newArray5[$e]; array_push($result52, $aux);
}
}
//41
for($d = 0; $d < count($newArray4); $d++){
for($a = 0; $a < count($newArray1); $a++){
$aux = $newArray4[$d].$newArray1[$a]; array_push($result52, $aux);
}
}
//42
for($d = 0; $d < count($newArray4); $d++){
for($b = 0; $b < count($newArray2); $b++){
$aux = $newArray4[$d].$newArray2[$b]; array_push($result52, $aux);
}
}
//43
for($d = 0; $d < count($newArray4); $d++){
for($c = 0; $c < count($newArray3); $c++){
$aux = $newArray4[$d].$newArray3[$c]; array_push($result52, $aux);
}
}
//45
for($d = 0; $d < count($newArray4); $d++){
for($e = 0; $e < count($newArray5); $e++){
$aux = $newArray4[$d].$newArray5[$e]; array_push($result52, $aux);
}
}
//51
for($e = 0; $e < count($newArray5); $e++){
for($a = 0; $a < count($newArray1); $a++){
$aux = $newArray5[$e].$newArray1[$a]; array_push($result52, $aux);
}
}
//52
for($e = 0; $e < count($newArray5); $e++){
for($b = 0; $b < count($newArray2); $b++){
$aux = $newArray5[$e].$newArray2[$b]; array_push($result52, $aux);
}
}
//53
for($e = 0; $e < count($newArray5); $e++){
for($c = 0; $c < count($newArray3); $c++){
$aux = $newArray5[$e].$newArray3[$c]; array_push($result52, $aux);
}
}
//54
for($e = 0; $e < count($newArray5); $e++){
for($d = 0; $d < count($newArray4); $d++){
$aux = $newArray5[$e].$newArray4[$d]; array_push($result52, $aux);
}
}
$result52 = array_unique($result52); //remove duplicates... but the indexes dont align.
$result52 = array_reduce($result52,"myfunction");//convert the array into a string bcos of the indexes
$result52 = str_split($result52, 2); //split the new string in twos into an array to get correct indexes
print_r($result52);
Спасибо всем.