strazdinjsh Posted July 22, 2011 Share Posted July 22, 2011 I am looking for help to make a function who returns the all possible variations without repetitions of the 2D array elements. I have done it easily with 1D array like: $array = array('a', 'b', 'c', 'd'); print_r(variations($array)); function variations($array){ $size = count($array); $data = array(); for($i=0; $i<$size; $i++) { foreach($array as $key=>$val) $data[] = array_slice($array, 0, $key+1); array_shift($array); } return $data; }; Have no luck to do so with 2D array what is required for the certain part of the web. The result must look something like: $array = array( array('a1', 'a2', 'a3'), array('b1', 'b2', 'b3'), array('c1', 'c2', 'c3'), ... ... array('x1', 'x2', 'x3') ); ################################# 1 variable a1 a2 a3 b1 b2 b3 c1 c2 c3 ########################################################## 2 variables a1 b1 a1 b2 a1 b3 a2 b1 a2 b2 a2 b3 a3 b1 a3 b2 a3 b3 ----------------- a1 c1 a1 c2 a1 c3 a2 c1 a2 c2 a2 c3 a3 c1 a3 c2 a3 c3 ----------------------------------------- b1 c1 b1 c2 b1 c3 b2 c1 b2 c2 b2 c3 b3 c1 b3 c2 b3 c3 ############################################################ 3 variables a1 b1 c1 a1 b2 c1 a1 b3 c1 a1 b2 c1 a1 b2 c2 a1 b2 c3 a1 b3 c1 a1 b3 c2 a1 b3 c3 ------------------------------------------------------------- a2 b1 c1 a2 b2 c1 a2 b3 c1 a2 b2 c1 a2 b2 c2 a2 b2 c3 a2 b3 c1 a2 b3 c2 a2 b3 c3 ------------------------------------------------------------ a3 b1 c1 a3 b2 c1 a3 b3 c1 a3 b2 c1 a3 b2 c2 a3 b2 c3 a3 b3 c1 a3 b3 c2 a3 b3 c3 -------------------------------------------------------------- ############################################################## n variables ############################################################## n variables Any help and advices would be appreciated. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/ Share on other sites More sharing options...
teynon Posted July 22, 2011 Share Posted July 22, 2011 Please further explain your expected input and expected output to be for the 2d array. Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246241 Share on other sites More sharing options...
strazdinjsh Posted July 22, 2011 Author Share Posted July 22, 2011 such array as matrix A1 A2 A3 B1 B2 B3 C1 C2 C3 must return the array containing all possible combinations of the matrix without repetitions 1. no pairs must return each array element seperately 2. 1 pair (2 elements) must return smth like A1B1-A1B2-A1B3 A2B1-A2B2-A2B3 ... ... ... 3. 3 (or size of array) elements must return A1B1C1-A1B2C1-A1B3C1 A2B1C1-A2B2C1-A2B3C1 ... ... ... Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246330 Share on other sites More sharing options...
teynon Posted July 22, 2011 Share Posted July 22, 2011 <?php $array = array( array('a1', 'a2', 'a3'), array('b1', 'b2', 'b3'), array('c1', 'c2', 'c3'), array('x1', 'x2', 'x3') ); function getCombinations($array, $string="", $i=0, $g=0, $data=array()) { $i++; $count=count($array); if ($g < $count) { for ($x=$g; $x < $count; $x++) { foreach ($array[$x] as $value) { $data[$i][]=$string.$value; $data=getCombinations($array, $string.$value."-", $i, $x+1, $data); } } } return $data; } $myData=getCombinations($array); echo "<pre>"; print_r($myData); echo "</pre>"; ?> Be aware that if you add any more elements to that array, you are exponentially increasing the amount of results and could end up in a very long process. Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246375 Share on other sites More sharing options...
teynon Posted July 22, 2011 Share Posted July 22, 2011 Update: Reread how you wanted output paired. This is probably closer to what you wanted. You can then condense the arrays however you want. <?php $array = array( array('a1', 'a2', 'a3'), array('b1', 'b2', 'b3'), array('c1', 'c2', 'c3'), array('x1', 'x2', 'x3') ); function getCombinations($array, $string="", $i=0, $g=0, $data=array(), $root=true, $key="") { $i++; $count=count($array); if ($g < $count) { for ($x=$g; $x < $count; $x++) { foreach ($array[$x] as $value) { if ($root) { $key=$value; } if (!isset($data[$key][$i])) { $data[$key][$i]=""; } if (empty($data[$key][$i])) { $data[$key][$i]=$string.$value; } else { $data[$key][$i].="-".$string.$value; } $data=getCombinations($array, $string.$value, $i, $x+1, $data, false, $key); } } } return $data; } $myData=getCombinations($array); echo "<pre>"; print_r($myData); echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246378 Share on other sites More sharing options...
xyph Posted July 22, 2011 Share Posted July 22, 2011 You realize how complex this is, correct? @ teynon - Your function misses a LOT of permutations. @ strazdinjsh - Do you consider a1b1c1 and a1c1b1 to be the same? Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246379 Share on other sites More sharing options...
teynon Posted July 22, 2011 Share Posted July 22, 2011 xyph, from the limited amount of information I've seen, he doesn't want repeats like x1a1b1 and what not. But I don't know for sure exactly what he wants. strazdinjsh, what do you want? Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246381 Share on other sites More sharing options...
xyph Posted July 22, 2011 Share Posted July 22, 2011 Look at the output of your function Array ( [a1] => Array ( [1] => a1 [2] => a1x3 [3] => a1c3x3 [4] => a1b3c3x3 ) [a2] => Array ( [1] => a2 [2] => a2x3 [3] => a2c3x3 [4] => a2b3c3x3 ) [a3] => Array ( [1] => a3 [2] => a3x3 [3] => a3c3x3 [4] => a3b3c3x3 ) [b1] => Array ( [1] => b1 [2] => b1x3 [3] => b1c3x3 ) [b2] => Array ( [1] => b2 [2] => b2x3 [3] => b2c3x3 ) [b3] => Array ( [1] => b3 [2] => b3x3 [3] => b3c3x3 ) [c1] => Array ( [1] => c1 [2] => c1x3 ) [c2] => Array ( [1] => c2 [2] => c2x3 ) [c3] => Array ( [1] => c3 [2] => c3x3 ) [x1] => Array ( [1] => x1 ) [x2] => Array ( [1] => x2 ) [x3] => Array ( [1] => x3 ) ) There isn't an a1b1x1 anywhere. In any order. Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246382 Share on other sites More sharing options...
teynon Posted July 22, 2011 Share Posted July 22, 2011 xyph, also don't know if he wants to skip entries or not. By the way, I updated that second function. Array ( [a1] => Array ( [1] => a1 [2] => a1b1-a1b2-a1b3-a1c1-a1c2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-a3x2-a3x3 [3] => a3b1c1-a3b1c2-a3b1c3-a3b1x1-a3b1x2-a3b1x3-a3b2c1-a3b2c2-a3b2c3-a3b2x1-a3b2x2-a3b2x3-a3b3c1-a3b3c2-a3b3c3-a3b3x1-a3b3x2-a3b3x3-a3c1x1-a3c1x2-a3c1x3-a3c2x1-a3c2x2-a3c2x3-a3c3x1-a3c3x2-a3c3x3 [4] => a3b1c1x1-a3b1c1x2-a3b1c1x3-a3b1c2x1-a3b1c2x2-a3b1c2x3-a3b1c3x1-a3b1c3x2-a3b1c3x3-a3b2c1x1-a3b2c1x2-a3b2c1x3-a3b2c2x1-a3b2c2x2-a3b2c2x3-a3b2c3x1-a3b2c3x2-a3b2c3x3-a3b3c1x1-a3b3c1x2-a3b3c1x3-a3b3c2x1-a3b3c2x2-a3b3c2x3-a3b3c3x1-a3b3c3x2-a3b3c3x3 ) [b1] => Array ( [1] => b1 [2] => b1c1-b1c2-b1c3-b1x1-b1x2-b1x3 [3] => b1c1x1-b1c1x2-b1c1x3-b1c2x1-b1c2x2-b1c2x3-b1c3x1-b1c3x2-b1c3x3 ) [b2] => Array ( [1] => b2 [2] => b2c1-b2c2-b2c3-b2x1-b2x2-b2x3 [3] => b2c1x1-b2c1x2-b2c1x3-b2c2x1-b2c2x2-b2c2x3-b2c3x1-b2c3x2-b2c3x3 ) [b3] => Array ( [1] => b3 [2] => b3c1-b3c2-b3c3-b3x1-b3x2-b3x3 [3] => b3c1x1-b3c1x2-b3c1x3-b3c2x1-b3c2x2-b3c2x3-b3c3x1-b3c3x2-b3c3x3 ) [c1] => Array ( [1] => c1 [2] => c1x1-c1x2-c1x3 ) [c2] => Array ( [1] => c2 [2] => c2x1-c2x2-c2x3 ) [c3] => Array ( [1] => c3 [2] => c3x1-c3x2-c3x3 ) [x1] => Array ( [1] => x1 ) [x2] => Array ( [1] => x2 ) [x3] => Array ( [1] => x3 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/242632-2d-array-variations-output/#findComment-1246383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.