简体   繁体   English

生成数组之间元素的所有组合

[英]Generate all combinations of elements between arrays

I have multiple arrays that contain multiple elememts, for example: 我有多个包含多个elememts的数组,例如:

$array1 = (1,2)
$array2 = (3,4)

I need to generate all possible combinations for elements of arrays. 我需要为数组的元素生成所有可能的组合。 For given example, the output should be: 对于给定的示例,输出应为:

1_3, 1_4, 2_3, 2_4

However, the issue is that both number of arrays and amount of elements in each one can be different. 但是,问题在于数组的数量和每个数组中元素的数量可能不同。 So we can also take something like that: 所以我们也可以采取这样的做法:

$array1 = (1,2,3,4) 
$array2 = (5,6,7) 
$array3 = (8,9,10,11,12) 

and output should look like that: 和输出应如下所示:

1_5_8, 1_5_9, 1_5_10 etc. until 4_7_12.

How do I implement something like that? 我该如何实现呢? I know that I should use foreach loop, but I have no idea what to do if amount of "foreaches" can be different every time I execute algorithm. 我知道我应该使用foreach循环,但是我不知道如果每次执行算法时“前奏”的数量可以不同,该怎么办。

Any help is really appreciated :) 任何帮助都非常感激:)

try something like this: 尝试这样的事情:

$array1 = array(1,2,3);
$array2 = array(4,5,6);
foreach ($array1 as $a1){
    foreach ($array2 as $a2){
       echo $a1 . '_' . $a2 . ', ';
    }
}

You can expand it like you wish. 您可以随意扩展它。

<?php 


$array1 = [1,2,3,4];
$array2 = [5,6,7];
$array3 = [8,9,10,11,12];

$collection = [$array1,$array2,$array3];

$result = $collection[0];
array_shift($collection);
foreach($collection as $each_array){
    $temp = [];
        foreach($result as $each_sub_result){
            foreach($each_array as $each_item){
                $temp[] = $each_sub_result."_".$each_item;
            }
        }    
    $result = $temp;
}

print_r($result);

Algorithm: 算法:

  • We collect all arrays in our $collection variable. 我们在$collection变量中收集所有数组。
  • Now, we loop over all elements of our $collection variable where each individual item is an array. 现在,我们遍历$collection变量的所有元素,其中每个项目都是一个数组。
  • I have done an array_shift() since we are assigning first element of $collection to $result (so we don't want to iterate over them again). 我已经完成了array_shift(),因为我们将$collection第一个元素分配给$result (所以我们不想再次遍历它们)。
  • We maintain $temp to store temporary results. 我们维持$temp来存储临时结果。 Note that we use $result also to store temporary results, so that we can iterate over it and get new temporary results. 请注意,我们还使用$result存储临时结果,以便我们可以对其进行迭代并获得新的临时结果。 By temporary , I mean building the final array. 临时的 ,我的意思是建立最终的数组。 For example: It starts with 1 , then 1_5 and then finally 1_5_8 . 例如:它从1开始,然后是1_5 ,最后是1_5_8
  • After the looping completes, we will have our final answer in $result . 循环完成后,我们将在$result得到最终答案。

I'm good in JS so that's why I did the algo in JS, you can try to do this in PHP,,no much difficult. 我对JS很好,所以这就是我在JS中做算法的原因,您可以尝试在PHP中做到这一点,没什么困难。 You just have to pass in all the array is the function and the rest should be done by itself. 您只需要传递函数的所有数组,其余的应该由它自己完成。 Let me know if any confusions. 让我知道是否有任何混淆。

    var arr1 = [1, 2, 3];
    var arr2 = [4, 5];
    var arr3 = [6, 7, 8];

    var numberOfArrays = -1;

    function processArrays(concatWith = [], allArrays) {
        const duplicateArray = [...allArrays];
        for (var i = 0; i < allArrays.length; i++) {
            const currentArray = duplicateArray.splice(0, 1);
            currentArray[0].forEach(function (value) {
                concatWith.push(value);
                processArrays(concatWith, duplicateArray);
                if (concatWith.length === numberOfArrays) {
                    console.log(concatWith.join('_'));
                }
                concatWith.pop();
            });
        }
    }

    function makeCombinations(arrayOfAllArrays = []) {

        numberOfArrays = arrayOfAllArrays.length;
        processArrays([], arrayOfAllArrays);
    }


    makeCombinations([arr1, arr2, arr3]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM