Combining arrays

0

Well, I'll try to explain a little better as you asked.

I have 3 arrays simple, I'm using an example with 3 arrays , but in fact the arrays number is not set, since they are set from the 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]

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 best if it could be done in JS but could be PHP as well.

    
asked by anonymous 19.10.2017 / 21:13

3 answers

2

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

See 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);
    
19.10.2017 / 23:26
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);

?>
    
19.10.2017 / 23:02
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);
    
19.10.2017 / 23:05