我有四个不同的阵列,每个阵列都有不同的长度。我想以下列方式遍历每个数组。 (注意:为了便于使用,我只在每个数组中包含少量值,无论数组的长度如何,解决方案都必须正常工作。)
$array1 = ["A","B","C"];
$array2 = ["D","E","F","G","H"];
$array3 = ["I","J","K","L","M"];
$array4 = ["N","O","P","Q"];
$combinedArray = [];
我需要$ combinedArray中的每个值包含一个唯一的字符串,该字符串由每个数组中的一个元素组成。 $ combinedArray应该具有与可能的唯一变体一样多的值。因此,使用上面的示例,$ combinedArray中应该有300(3 * 5 * 5 * 4)个值。
因此$ combinedArray中的每个值应类似于以下内容:
$combinedArray[0] = "ADIN";
$combinedArray[1] = "BDIN";
$combinedArray[2] = "CDIN";
$combinedArray[3] = "AEIN";
...
$combinedArray[MAX] = "CHMQ";
有关如何做到这一点的任何想法?感谢。
答案 0 :(得分:0)
试试这个: -
$array1 = ["A","B","C"];
$array2 = ["D","E","F","G","H"];
$array3 = ["I","J","K","L","M"];
$array4 = ["N","O","P","Q"];
$combinedArray = [];
foreach ($array1 as $a1){
foreach ($array2 as $a2){
foreach ($array3 as $a3){
foreach ($array4 as $a4){
$combinedArray[] = $a1 . $a2 . $a3 . $a4;
}
}
}
}
没什么特别的。刚刚嵌套的循环。希望这会有所帮助。