I have been searching and scratching my head for days and I'm stuck between a rock and a hard place. After all my previous code runs, I am left with this array:
Array (
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 8
)
[2] => Array
(
[0] => 9
[1] => 10
)
)
What I need to do, is calculate every possible permutation of all keys.
The desired output needs to be easily insertable as either individual records or preferably a bulk insert into a sql database.
After my hunting around, I have tried countless examples. The closest I got was using an Implode function however this still wouldn't work.
Any help is greatly appreciated!
--Edit--
Here is an example of how the returned array should look:
Array
(
[0] => 1,6,9
[1] => 1,6,10
[2] => 3,6,9
[3] => 3,6,10
[4] => 1,7,9
[5] => 1,7,10
[6] => 3,7,9,
[7] => 3,7,10
)
This isn't every permutation, but should give you an idea of what I need to achieve.
What you are trying to do is defined in this formula: 
And it might be done like that, it's called a Cartesian product:
function cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
$count = cartesian(
Array(1,3),
Array(6,7,8),
Array(9,10)
);
print_r($count);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With