NancyM Posted June 17, 2009 Share Posted June 17, 2009 I read this thread and it almost solves my problem: http://www.phpfreaks.com/forums/index.php?topic=213809.0 but not quite. I want to preserve an order to my permutations. For instance my data set is like this: $arr = array(0=>array("tee shirt","golf clubs","bread"), 1=>array("blue", "black", "brown"), 2=>array("large", "medium","small")); I want to have all combinations of items, colors and sizes but they need to stay in the order of item - color - size. Thanks for any insight! Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/ Share on other sites More sharing options...
Mark Baker Posted June 17, 2009 Share Posted June 17, 2009 $arr = array(0=>array("tee shirt","golf clubs","bread"), 1=>array("blue", "black", "brown"), 2=>array("large", "medium","small")); foreach($arr[0] as $item) { foreach($arr[1] as $colour) { foreach($arr[2] as $size) { echo $item.' '.$colour.' '.$size.'<br />'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-858381 Share on other sites More sharing options...
Daniel0 Posted June 17, 2009 Share Posted June 17, 2009 Alternately like this for an arbitrary amount of arrays: function permutations(array $array) { switch (count($array)) { case 1: return $array[0]; break; case 0: throw new InvalidArgumentException('Requires at least one array'); break; } $a = array_shift($array); $b = permutations($array); $return = array(); foreach ($a as $v) { foreach ($b as $v2) { $return[] = array_merge(array($v), (array) $v2); } } return $return; } $arr = array( array("tee shirt","golf clubs","bread"), array("blue", "black", "brown"), array("large", "medium","small"), ); print_r(permutations($arr)); And in Haskell, just for fun: [ (item, color, size) | item <- ["tee shirt","golf clubs","bread"], color <- ["blue", "black", "brown"], size <- ["large", "medium","small"] ] or generally: perm [] = [[]] perm (x:xs) = [ x':xs' | x' <- x, xs' <- perm xs ] Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-858398 Share on other sites More sharing options...
NancyM Posted June 17, 2009 Author Share Posted June 17, 2009 Woot! Thanks folks! I especially love the example for the one with arbitrary amount of arrays. Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-858449 Share on other sites More sharing options...
GingerRobot Posted June 18, 2009 Share Posted June 18, 2009 And in Haskell, just for fun: [ (item, color, size) | item <- ["tee shirt","golf clubs","bread"], color <- ["blue", "black", "brown"], size <- ["large", "medium","small"] ] or generally: perm [] = [[]] perm (x:xs) = [ x':xs' | x' <- x, xs' <- perm xs ] Haha. You're liking Haskell then? Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-858853 Share on other sites More sharing options...
Daniel0 Posted June 18, 2009 Share Posted June 18, 2009 I think it's a great language, but it's somewhat difficult wrapping your head around functional programming after having used the imperative paradigm for numerous years. Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-858856 Share on other sites More sharing options...
FlashT Posted February 17, 2010 Share Posted February 17, 2010 Daniel0: your example with permutation doesnt work correctly... could you please fix it because i have no idea how... for example: Array ( [0] => Array ( [0] => [1] => pp [2] => 9 [3] => 99 [4] => o [5] => oo [6] => l [7] => ll [8] => 0 [9] => 00 [10] => p [12] => - [13] => -- ) [1] => Array ( [0] => [1] => aa [2] => q [3] => qq [4] => a [6] => z [7] => zz [8] => w [9] => ww [10] => s [11] => ss [12] => x [13] => xx ) [2] => Array ( [0] => [1] => tt [2] => 4 [3] => 44 [4] => r [5] => rr [6] => f [7] => ff [8] => 5 [9] => 55 [10] => t [12] => g [13] => gg [14] => 6 [15] => 66 [16] => y [17] => yy [18] => h [19] => hh ) [3] => Array ( [0] => [1] => rr [2] => 3 [3] => 33 [4] => e [5] => ee [6] => d [7] => dd [8] => 4 [9] => 44 [10] => r [12] => f [13] => ff [14] => 5 [15] => 55 [16] => t [17] => tt [18] => g [19] => gg ) [4] => Array ( [0] => [1] => zz [2] => a [3] => aa [4] => z [6] => s [7] => ss [8] => x [9] => xx ) ) Your function for this array wont generate all permutations... For example there will be no: pattrz or even ppaattrrzz Best regards, FlashT Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-1013674 Share on other sites More sharing options...
FlashT Posted February 17, 2010 Share Posted February 17, 2010 My bad... i modified it to return array of words because i need to do somethng with them before returning, and doing it after takes too much ram... but was bad modification... could you please help me with that? function permutations(array $array) { switch (count($array)) { case 1: return $array[0]; break; case 0: throw new InvalidArgumentException('Requires at least one array'); break; } $a = array_shift($array); $b = permutations($array); $return = array(); foreach ($a as $v) { foreach ($b as $v2) { $word = implode('',array_merge(array($v), (array) $v2)); if(<something_with_word>) $return[] = implode('',array_merge(array($v), (array) $v2)); } } return $return; } How do I make it work correctly? Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-1013681 Share on other sites More sharing options...
Daniel0 Posted February 17, 2010 Share Posted February 17, 2010 Buy more RAM or see if can make it tail recursive to reduce the memory needed for the call stack. At any rate, you will need a lot of memory because of the factorial growth. At only 20 items, the number of permutations will be 243,2902,008,176,640,000. Also, please don't bump topics that are so old! Quote Link to comment https://forums.phpfreaks.com/topic/162637-ordered-permutations-of-a-2-dimensional-array/#findComment-1013688 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.