php - How to create a permutation of two dimensional array -


this question has answer here:

initially, afraid, not find better title question.

i have 2 dimensional array looks this, example:

 [0] => array      (         [0] => 10,00      )  [1] => array      (         [0] => 3         [1] => 4      )  [2] => array      (         [0] => true         [1] => false      ) 

i'd convert/parse 2 dimensional array looks this:

[0] => array     (         [0] => 10,00         [1] => 3         [2] => true     ) [1] => array     (         [0] => 10,00         [1] => 4         [2] => true     ) [2] => array     (         [0] => 10,00         [1] => 3         [2] => false     ) [3] => array     (         [0] => 10,00         [1] => 4         [2] => false     ) 

i hope see, result should provide sort of possible combinations. indeed, length of first array can differ.

i'd interested in how solve algorithmically, @ moment have no idea.

i not sure, if easy looks like. thank in advance.

i imagine refined, should trick:

<?php  $arrstart = array(     array('10,00'),     array(3, 4),     array('true', 'false') );  $arrpositions = array(); $arrresult = array();  //get starting position set each sub array ($i = 0; $i < count($arrstart); $i++)     $arrpositions[] = 0;  //repeat until we've run out of items in $arrstart[0] while (array_key_exists($arrpositions[0], $arrstart[0])) {     $arrtemp = array();     $blsuccess = true;      //go through each of first array levels     ($i = 0; $i < count($arrstart); $i++) {         //is there item in position want in current array?         if (array_key_exists($arrpositions[$i], $arrstart[$i])) {             //add item our temp array             $arrtemp[] = $arrstart[$i][$arrpositions[$i]];         } else {             //reset position, , raise 1 left             $arrpositions[$i] = 0;             $arrpositions[$i - 1]++;             $blsuccess = false;         }     }      //this 1 failed due there not being item wanted, skip next go     if (!$blsuccess) continue;      //successfully adding nex line, increase right hand count next 1     $arrpositions[count($arrstart) - 1]++;      //add our latest temp array result     $arrresult[] = $arrtemp; }  print_r($arrresult); ?> 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -