Combine arrays

Asked

Viewed 203 times

1

Well, I’ll try to explain a little better as you requested.

I own 3 arrays simple, I’m using an example with 3 arrays, but actually the number of arrays is not defined as they are mounted from user’s choices, so I can have 2 to 5 arrays depending on what the user chooses. But most would be 3 arrays: (Decreases the size of array to make it easier to understand)

[A,B]
[1,2]
[x,y]

And I need to make a combination between them so that something like:

[A,1,x]
[A,1,y]
[A,2,x]
[A,2,y]
[B,1,x]
[B,1,y]
[B,2,x]
[B,2,y]

The output must be a new array with the relation between all the values of the previous ones. It would be better if it could be done in JS but it may be for PHP also.

  • It is not very clear which is the combination rule of Phlegm, can describe to us?

  • And you want in JS or PHP?

  • I made some corrections to explain better.

3 answers

2


Making a solution in the style of @Vinicius, but using ES6 syntax would be much more compact. For this you can use forEach and Arrow Functions.

Take the example:

const v1 = ["A","B","C"], v2 = [1,2,3], v3 = ["x","y","z"], res = [];

v1.forEach(x=>v2.forEach(y=>v3.forEach(z=>res.push([x,y,z]))));

console.log(res);

  • It would have to leave "automatic" even changing the number of arrays?

  • @Fleuquerlima Da to make "automatic" in many ways depending on how you are using it in the code. One of the most direct would be to make a function with this code and call this function whenever it changes the original arrays.

1

An ugly example:

<?php

  $array1 = array('A', 'B', 'C');
  $array2 = array(1, 2, 3);
  $array3 = array('x', 'y', 'z');

  $output = array();
  foreach ($array1 as $value1) {
    foreach ($array2 as $value2) {
      foreach ($array3 as $value3){
        array_push($output, array($value1, $value2, $value3));
      }
    }
  }

  print_r($output);

?>

1

I made a JS version

    var array1 = ["A","B","C"];
    var array2 = [1,2,3];
    var array3 = ["x","y","z"];
    var array4 = [22,33,44];
    var array5 = ["te","s","t"];

    // fazer um array para receber todos os arrays dentro dele
    var conjArray = [array1,array2,array3];
    var tam = conjArray.length;
    var array = [];


    for (var iterable_element1 of array1) {
        for (var iterable_element2 of array2) {
            if (tam == 2) {
                var newarray = [] ;
                newarray.push(iterable_element1);
                newarray.push(iterable_element2);
                array.push(newarray);
            }
            for (var iterable_element3 of array3) {
                if (tam == 3) {
                    var newarray = [] ;
                    newarray.push(iterable_element1);
                    newarray.push(iterable_element2);
                    newarray.push(iterable_element3);
                    array.push(newarray);
                }
                for (var iterable_element4 of array4) {
                    if (tam == 4) {
                        var newarray = [] ;
                        newarray.push(iterable_element1);
                        newarray.push(iterable_element2);
                        newarray.push(iterable_element3);
                        newarray.push(iterable_element4);
                        array.push(newarray);
                    }
                    for (var iterable_element5 of array5) {
                        if (tam == 5) {
                            var newarray = [] ;
                            newarray.push(iterable_element1);
                            newarray.push(iterable_element2);
                            newarray.push(iterable_element3);
                            newarray.push(iterable_element4);
                            newarray.push(iterable_element5);
                            array.push(newarray);
                        }
                    }
                }
            }
        }
    }
    console.log(array);
  • It would have to leave "automatic" even changing the number of arrays?

  • @Fleuquerlima I will edit my reply , since you said that at least of arrays is 2 and the maximum is 5 the best I got was this

Browser other questions tagged

You are not signed in. Login or sign up in order to post.