PHP组合两个或多个数组

参见英文答案 >
How to get all combinations from multiple arrays?                                    2个

嗨大家我不能在PHP之间做.请帮我..

我有这些数组.

$arr1=[9,47]

$arr2=[15,113]

$arr3=[42,69]

//dynamically may be there is more or less array.

我想像这样创建数组,意味着在所有数组上组合每个值

            //firstArray_firstValue, secondArray_firstValue, thirdArray_firstValue, ...
            //firstArray_firstValue, secondArray_firstValue, thirdArray_secondValue, ...
            //...
            //firstArray_firstValue, secondArray_secondValue, thirdArray_firstValue, ...
            //firstArray_firstValue, secondArray_secondValue, thirdArray_secondValue, ...
            //...
            //firstArray_secondValue, secondArray_firstValue, thirdArray_firstValue, ...
            //firstArray_secondValue, secondArray_firstValue, thirdArray_secondValue, ...
            //...
            //firstArray_secondValue, secondArray_secondValue, thirdArray_firstValue, ...
            //firstArray_secondValue, secondArray_secondValue, thirdArray_secondValue, ...

这些数组的示例结果

$resultArray = [
        [9, 15, 42],
        [9, 15, 69],
        [9, 113, 42],
        [9, 113, 69],
        [47, 15, 42],
        [47, 15, 69],
        [47, 113, 42],
        [47, 113, 69],
    ];

如何动态创建此结果?

最佳答案 这里是:

<?php

$arr1=[9,47];
$arr2=[15,113];
$arr3=[42,69];

foreach($arr1 as $a1)
    foreach($arr2 as $a2)
        foreach($arr3 as $a3)
            $resultArray[] = [$a1, $a2, $a3];


print_r($resultArray);
点赞