Es scheint, als ob keine der Antworten, einschließlich der akzeptiert man, wird funktionieren, wenn es zwei Arrays gleicher Länge gibt. Ich habe die akzeptierte Antwort persönlich getestet und festgestellt, dass dies der Fall ist, und nach den Kommentaren zu den anderen beiden haben sie das gleiche Problem.
Ich musste diesen Algorithmus vor kurzem implementieren, also werde ich meine Lösung posten. Diese Lösung wurde für die Verwendung mit assoziativen Arrays entwickelt und unterstützt auch Gruppen von Spalten, die in der Ausgabe zusammen gruppiert und nicht gegeneinander vertauscht werden. Dies ist nützlich, wenn eine der Spalten verwandte Informationen enthält. Wenn Sie diese Funktionen nicht benötigen, sollte es ziemlich trivial sein, diesen Algorithmus so zu modifizieren, dass er Ihren Anforderungen entspricht.
// the input column sets to be permuted
$column_sets = [
[ //set 1
['Column 1' => 'c1v1']
],
[ //set 2
['column 2' => 'c2v1', 'Column 3' => 'c3v1'],
['column 2' => 'c2v2', 'Column 3' => 'c3v2'],
],
[ //set 3
['Column 4' => 'c4v1', 'Column 5' => 'c5v1'],
['Column 4' => 'c4v2', 'Column 5' => 'c5v2'],
],
[ //set 4
['Column 6' => 'c6v1', 'Column 7' => 'c7v1'],
['Column 6' => 'c6v2', 'Column 7' => 'c7v2'],
['Column 6' => 'c6v3', 'Column 7' => 'c7v3'],
],
[ //set 5
['Column 8' => 'c8v1', 'Column 9' => 'c9v1'],
['Column 8' => 'c8v2', 'Column 9' => 'c9v2'],
['Column 8' => 'c8v3', 'Column 9' => 'c9v3'],
],
];
// copy the first set into the output to start
$output_rows = $column_sets[0];
foreach ($column_sets as $column_set) {
// save the current state of the rows for use within this loop
$current_output = $output_rows;
// calculate the number of permutations necessary to combine the output rows with the current column set
$permutations = count($output_rows) * count($column_set);
for ($permutation=0; $permutation < $permutations; $permutation++) {
// if the current permutation index is grater than the number of rows in the output,
// then copy a row from the pre-permutation output rows, repeating as necessary
if ($permutation > count($output_rows) - 1) {
$output_rows[] = $current_output[$permutation % count($current_output)];
}
// copy the columns from the column set
foreach ($column_set[0] as $key => $value) {
$output_rows[$permutation][$key] = $column_set[intval($permutation/count($current_output)) % count($column_set)][$key];
}
}
}
echo "After Permutaion:\n";
echo implode("\t", array_keys($output_rows[0])) . PHP_EOL;
foreach ($output_rows as $row) {
echo implode("\t", array_values($row)) . PHP_EOL;
}
Ist die Reihenfolge wichtig? – Carlos
@jackflash Es ist vertikal nicht wichtig, aber horizontal wichtig. – PHPst