php - How to get all combinations from multiple arrays? -


supposing have these 3 arrays

$array1 = array(1,2); $array2 = array(4,5); $array3 = array(7,8); 

i need output

1 4 7 1 4 8 1 5 7 1 5 8 2 4 7 2 4 8 2 5 7 2 5 8 

one of problems array myght vary 3 15 different arrays , each myght empty (i might add 0 not empty) or have many values. if have empty array need count valid column. these values used fill database in specific order.

is there way can this?

how many combinations there?

so first question how many combinations there? , answer have multiply amount of every array each other.

so (c = amount1):

carray 1 * carray 2 * ... * carray n

and specific example:

carray 1 * carray 2 * carray 3 = 2 * 2 * 2 = 8

*1 , if wonder why chose c amount, because of function count() in php

getting combinations together

how combinations amount of arrays, have?

we loop through our combinations, have(starting off 1 combination, "empty combination" ($combinations = [[]];)), , each combination go through our next data array , combine each combination each input data new combination.

now until desired length each combination.

so example:

array elements (empty array '[]'):  [     [1, 2],     [3, 4] ] 

                               //↓ new combinations next iteration                                │ array nan*:      combinations:                   - []         │  -> []                                   │ array 1 [1,2]:      ┌─────────────┤                     │             │     combinations:   v             v                   - []    + 1  │  -> [1]                     - []    + 2  │  -> [2]                                      │ array 2 [3,4]:      ┌─────────────┤                     │             │     combinations:   v             v                   - []    + 3  │  -> [3]                   - []    + 4  │  -> [4]                   - [1]   + 3  │  -> [1,3]  //desired length 2 have 2 arrays                    - [1]   + 4  │  -> [1,4]  //desired length 2 have 2 arrays                    - [2]   + 3  │  -> [2,3]  //desired length 2 have 2 arrays                    - [2]   + 4  │  -> [2,4]  //desired length 2 have 2 arrays                                    //↑ combinations here 

* nan: not number

so can see in above example have combinations length of amount of arrays have.

but combinations desired length overwriting result array each iteration, @ end combinations expected length in results array.

code:

<?php      $array1 = array(1,2);     $array2 = array(4,5);     $array3 = array(7,8);       $combinations = [[]];     $data = [         $array1,         $array2,         $array3,     ];     $length = count($data);      ($count = 0; $count < $length; $count++) {         $tmp = [];         foreach ($combinations $v1) {             foreach ($data[$count] $v2)                 $tmp[] = array_merge($v1, [$v2]);          }         $combinations = $tmp;     }      print_r($combinations);  ?> 

output:

array (     [0] => array         (             [0] => 1             [1] => 4             [2] => 7         )     //...     [7] => array         (             [0] => 2             [1] => 5             [2] => 8         )  ) 

for associative arrays have slight modification, is:

  1. first assign arrays keys variable array_keys(), e.g.

    $keys = array_keys($data); 
  2. use keys in second foreach loop access data array, means from:

    foreach ($data[$count] $v2) 

    to:

    foreach ($data[$keys[$count]] $v2)

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -