jcsmithy Posted April 14, 2011 Share Posted April 14, 2011 Hi PHPfreaks, I've got a problem Ive been faced with a few days now, and for the life of me Im unable to solve it. What I have is a multidimensional array, that I need to parse and retreive every possible outcome possible Below is a basic example of how the array would look Colour Black White Blue Finish Matt Gloss What I need to be able to do is, as I said, Show all the possible combinations there is. below is what i'd have achieved after one of you helpful souls points me in the right direction. --> Color: Black --> Color: White --> Color: Blue --> Finish: Matt --> Finish: Matt --> Color: Black, Finish: Matt --> Color: White, Finish: Matt --> Color: Blue, Finish: Matt --> Color: Black, Finish: Gloss --> Color: White, Finish: Gloss --> Color: Blue, Finish: Gloss As you can see, Only ONE (or no) value is picked from each section. Just to confirm: the number of option groups WILL change...... So I could have colour, finish, size, weight and many more. the number of values in the groups WILL change....... so under colour, I could also have pink, green, indigo, gray. Below is how this array is structured Array ( [0] => Array ( [parent] => Colour [values] => Array ( [0] => Array ( [name] => Black ) [1] => Array ( [name] => White ) ) ) [1] => Array ( [parent] => Finish [values] => Array ( [0] => Array ( [name] => Matt ) [1] => Array ( [name] => Gloss ) ) ) ) Would anyone have any suggestion on how this can be done? I've pulled out every single strand of hair I've got now, so thought i'd admit defeat and let someone else have a go Many thanks inadvance. Quote Link to comment https://forums.phpfreaks.com/topic/233729-finding-all-possible-combinations-of-an-array/ Share on other sites More sharing options...
requinix Posted April 14, 2011 Share Posted April 14, 2011 function combinations(array $input, $allowempty = false) { $combinations = array(); $copy = $input; foreach ($input as $key => $data) { unset($copy[$key]); $subcombs = combinations($copy, true); foreach ($data["values"] as $value) { $comb = array($data["parent"] => $value["name"]); $combinations[] = $comb; foreach ($subcombs as $subcomb) $combinations[] = array_merge($comb, $subcomb); } } return $combinations) } $input = array( array( "parent" => "Colour", "values" => array(array("name" => "Black"), array("name" => "White"), array("name" => "Blue")) ), array( "parent" => "Finish", "values" => array(array("name" => "Matt"), array("name" => "Gloss")) ), array( "parent" => "Size", "values" => array(array("name" => "Small"), array("name" => "Medium"), array("name" => "Large")) ) ); print_r(combinations($input)); The total number of combinations will be the product of (how many choices there are per thing + 1) - 1. Here it's (3+1)*(2+1)*(3+1)-1 = 47. Quote Link to comment https://forums.phpfreaks.com/topic/233729-finding-all-possible-combinations-of-an-array/#findComment-1201623 Share on other sites More sharing options...
Muddy_Funster Posted April 14, 2011 Share Posted April 14, 2011 You do realise that as you increase the number of catagories the number of outputs will increase exponentialy, causing a MASSIVE dataset in a very short time, even just adding the 2 more catagories that you have given, increasing your colour selection to the 8 colours listed and duplicating the number of options (8 and 4) for the next two catagories (size and weight) you would end up with 35,184,372,088,832 results being output by that code...Are you tring to kill somones server? Quote Link to comment https://forums.phpfreaks.com/topic/233729-finding-all-possible-combinations-of-an-array/#findComment-1201632 Share on other sites More sharing options...
jcsmithy Posted April 14, 2011 Author Share Posted April 14, 2011 Must say Im suprsed at the speed of your answer, within the hour yet this had me stumped for days! I would call you a genius, but due to the typo, not sure I can return $combinations) And, No I didnt realised it'd create that many - will have to put some limits in place I think! the chances of having that many values would be slim anyway...... I hope BIG thanks to you requinix anyway, been a massive help. Quote Link to comment https://forums.phpfreaks.com/topic/233729-finding-all-possible-combinations-of-an-array/#findComment-1201636 Share on other sites More sharing options...
requinix Posted April 15, 2011 Share Posted April 15, 2011 I would call you a genius, but due to the typo, not sure I can return $combinations) Ah. It used to have an array_merge there but I redid some stuff and got rid of it... mostly. And, No I didnt realised it'd create that many - will have to put some limits in place I think! the chances of having that many values would be slim anyway...... I hope Using what you've mentioned as an example: - Color: red, orange, yellow, green, blue, purple, black, white, brown, silver (10) - Finish: none, matte, gloss, clear, shiny, dull (6) - Size: x-large, large, medium, small, x-small (5) - Weight: heavy, normal, light (3) 1848 combinations. Want to add a color? 2016. Add another category with three choices? 7392. Quote Link to comment https://forums.phpfreaks.com/topic/233729-finding-all-possible-combinations-of-an-array/#findComment-1202165 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.