【问题标题】:PHP combine two or more array [duplicate]PHP组合两个或多个数组[重复]
【发布时间】:2018-12-05 15:13:55
【问题描述】:

大家好,我不能在 php 上做 betweens。请帮帮我..

我有这些数组。

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

如何动态创建此结果?

【问题讨论】:

  • 您知道构建该结果的逻辑,对吗?因此,您可以尝试自己做一些事情。到目前为止,您尝试过什么?
  • 您可能需要使用循环
  • 试试 array_merge 或 array_map,也许其中一种对你有用。
  • @FelippeDuarte 我只尝试了 foreach 循环中的 foreach 循环。但我无法接近解决方案。我无法解决我应该建立什么样的结构。
  • @PatrickQ 谢谢。它解决了我的问题..

标签: php arrays function


【解决方案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);

【讨论】:

  • 感谢您的回答。但是如果有或多或少的数组,这个方案就不可用了。
【解决方案2】:

具体为您的案例解决方案是:

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

【讨论】:

  • 感谢您的回答。但是如果数组或多或少,这个方案就不可用了。
  • 你当然可以通过变量名进行迭代,但这根本不是一个好习惯,你还需要在变量名中预先设置一些模式,或者至少将所有数组名保存在一些额外的数组中。
猜你喜欢
  • 2019-02-22
  • 1970-01-01
  • 2016-03-13
  • 2015-10-21
  • 2014-06-29
  • 2018-10-05
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多