Michael Diak
Michael Diak

Reputation: 33

Generate all combinations of elements between arrays

I have multiple arrays that contain multiple elememts, for example:

$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.

Any help is really appreciated :)

Upvotes: 0

Views: 1064

Answers (3)

usmany
usmany

Reputation: 144

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. 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]);

Upvotes: -1

nice_dev
nice_dev

Reputation: 17805

<?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.
  • Now, we loop over all elements of our $collection variable where each individual item is an array.
  • 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).
  • We maintain $temp to store temporary results. Note that we use $result also to store temporary results, so that we can iterate over it and get new temporary results. By temporary, I mean building the final array. For example: It starts with 1, then 1_5 and then finally 1_5_8.
  • After the looping completes, we will have our final answer in $result.

Upvotes: 1

Larchfomos
Larchfomos

Reputation: 11

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.

Upvotes: 1

Related Questions