Any tips on how to do something like this? I have an array like this:
array (
[a] => array(1,2)
[b] => array(4,5)
[c] => array(y,z)
)
and I want to multiply the keys to get a result like this:
array (
[0]=> array (
[a]=1
[b]=4
[c]=y
)
[1]=> array (
[a]=1
[b]=4
[c]=z
)
[2]=> array (
[a]=1
[b]=5
[c]=y
)
[3]=> array (
[a]=1
[b]=5
[c]=z
)
[4]=> array (
[a]=2
[b]=4
[c]=y
)
[5]=> array (
[a]=2
[b]=4
[c]=z
)
.
.
.
Where all combinations of the keys are present. The number of keys and elements will be variable, and it's important to preserve the keys.
If I knew how many keys I was dealing with I'd do something like this:
$return = array();
$i=0;
foreach ($array[a] as $val_a) {
foreach ($array[b] as $val_b) {
foreach ($array[c] as $val_c) {
$return[$i][a] = $val_a;
$return[$i][b] = $val_b;
$return[$i][c] = $val_b;
$i++;
}
}
}
I'm sure there's a recursive function to do this, but I can't quite figure out how to do it.