Jump to content

Finding all possible combinations of an Array


jcsmithy

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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? :o

Link to comment
Share on other sites

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  ;D

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  :o

 

 

BIG thanks to you requinix anyway, been a massive help.

Link to comment
Share on other sites

I would call you a genius, but due to the typo, not sure I can  ;D

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  :o

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.