PHP结合两个或多个数组

时间:2018-12-05 15:13:56

标签: php arrays function

大家好,我不能在php上做weens。请帮助我。

我有这些数组。

$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],
    ];

如何动态创建此结果?

2 个答案:

答案 0 :(得分:1)

在这里:

<?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);

答案 1 :(得分:0)

针对您的案例解决方案的具体做法是:

$resultArray = [];

foreach ($arr1 as $arr1_item){
    foreach ($arr2 as $arr2_item) {
        foreach ($arr3 as $arr3_item){
            $resultArray[] = [
                $arr1_item,
                $arr2_item,
                $arr3_item,
            ];
        }
    }
}

[更新]

为大于2的任意数量的数组添加实现。 数组必须命名为:$ arr1,$ arr2 .... $ arr {N}

<?php

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

$array_name_pattern = 'arr';
$iterator = 1;
$array_names = [];

while (isset(${'arr' . $iterator})) {
    $array_names[] = 'arr' . $iterator;
    $iterator++;
}

$resultArray = [];


if (count($array_names) > 1) {  // just checking if we have enough arrays
    // initiate first elements and all lines
    $can = 1; // combinations for each element in first array


    // calculate combinations
    for ($chi = 2; $chi <= count($array_names); $chi++) {
        $can *= count(${'arr' . $chi});
    }

    // create all lines with first element
    //    9  x  x
    //    9  x  x
    //    9  x  x       in our case
    //    9  x  x
    //    47 x  x
    //    47 x  x
    //    47 x  x
    //    47 x  x

    foreach (${'arr' . 1} as $current_array_item) {
        for ($lhi = 1; $lhi <= $can; $lhi++) {
            $resultArray[] = [$current_array_item];
        }
    }

    // iterate trough all remained arrays
    for ($i = 2; $i <= count($array_names); $i++) {
        $gi = 0; // resulting array line number
        $can = 1; // combinations for each element of current array
        $chi = $i + 1;

        // calculate combinations
        for ($chi; $chi <= count($array_names); $chi++) {
            $can *= count(${'arr' . $chi});
        }

        while($gi < count($resultArray)) { // repeating for all lines in resulting array
            foreach (${'arr' . $i} as $current_array_item) { // get each item in current array
                for ($lhi = 1; $lhi <= $can; $lhi++) { // repeat combinations

                    //    9  15   x
                    //    9  15   x
                    //    9  113  x       2nd array, 2 items, 2 combinations per item
                    //    9  113  x
                    //    47  x   x
                    //    47  x   x
                    //    47  x   x
                    //    47  x   x

                    $resultArray[$gi][] = $current_array_item;
                    $gi++;
                }
            }
        }
    }
}

print_r($resultArray);