I have an array:
$array[1] = ['a1','a2'];
$array[2] = ['b','c','d'];
I would like to achieve the following array:
['a1,'b']
['a1,'c']
['a1,'d']
['a1,'b','c']
['a1,'b','d']
['a1,'c','d']
['a1,'b','c','d']
['a2,'b']
['a2,'c']
['a2,'d']
['a2,'b','c']
['a2,'b','d']
['a2,'c','d']
['a2,'b','c','d']
!!important!!
The value of $array[1] and $array[2] can have more than 2 items. Can have X items.
So far , this is my script:
function getAllPermutations($array, $inb = false)
{
switch (count($array)) {
case 1:
return $array[0];
break;
case 0:
echo 'Requires at least one array';
break;
}
$keys = array_keys($array);
$a = array_shift($array);
$k = array_shift($keys);
$b = getAllPermutations($array, 'recursing');
$return = [];
foreach ($a as $value) {
if($value){
foreach ($b as $value2) {
if (!is_array($value2))
$value2 = [$value2];
if((string) $inb === 'recursing') {
$return[] = array_merge([$value], (array) $value2);
}else {
$return[] = [$k => $value] + array_combine($keys, $value2);
}
}
}
}
return $return;
}
and i get these values:
[0] => Array
(
[1] => a1
[2] => b
)
[1] => Array
(
[1] => a1
[2] => c
)
[2] => Array
(
[1] => a1
[2] => d
)
[3] => Array
(
[1] => a2
[2] => b
)
[4] => Array
(
[1] => a2
[2] => c
)
[5] => Array
(
[1] => a2
[2] => d
)
Any ideas on how should i fix this ? Thank you