I simply cannot wrap my head around how to solve this problem and after a thorough search on Google with no results, I turn to you with hopes of a solution.
Given the sample array below:
array(
'Type' => array(
'Toppe',
'Bukser_og_Jeans'
),
'Size' => array(
'Extra_small',
'Small'
),
'Colour' => array(
'Rod'
)
)
(Note: This is merely a sample; the actual real life situation might have less/more groups and/or elements per group)
How would I go about ending up with the following result?
Toppe,Extra_small,Rod
Toppe,Small,Rod
Bukser_og_Jeans,Extra_small,Rod
Bukser_og_Jeans,Small,Rod
This is a product search and the API only allows ONE 'refinement' value from each of the Type, Size and Colour groups per query but my assignment requires to query and aggregate the results of multiple API queries.
I'm thinking that I need some kind of recursive function to do it, but I have been unable to even produce any code that comes close to my expected result.
All I've been able to find on Google is about permuations of letters or even strings, but where people need e.g. "Red,Blue,Green", "Blue,Red,Green", "Green,Red,Blue", etc., which is, clearly, not what I'm looking for.
I hope someone here understands what I want to do and has an idea of how to do it.
EDIT: The solution as posted by @ikegami, converted to PHP:
$iter = 0;
while (1) {
$num = $iter++;
$pick = array();
foreach ($refinements as $refineGroup => $groupValues) {
$r = $num % count($groupValues);
$num = ($num - $r) / count($groupValues);
$pick[] = $groupValues[$r];
}
if ($num > 0) {
break;
}
print join(', ', $pick)."\n";
}