3

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.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Nicholas Mordecai
  • 859
  • 2
  • 12
  • 33
  • Do you want to switch dimensions? I mean that values from 3rd dimension can be used in first one and so on? – Cheery Nov 17 '14 at 23:19
  • @Cheery It's more important for me to get this code working. I can re-write the insert into the database according to the returned output format. Thanks – Nicholas Mordecai Nov 17 '14 at 23:51

1 Answers1

5

What you are trying to do is defined in this formula: permutation 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);
Tim Post
  • 33,371
  • 15
  • 110
  • 174
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • That's incredible! Thank you! One last question, would it be possible to use dynamic arrays in the "$count = printer(" If my original array = "$total" Could I use.. "$count = printer($total);" – Nicholas Mordecai Nov 18 '14 at 00:09
  • I am able to convert my array into the indexed arrays you use, however I may have multiple arrays. This needs to be dynamic. I tried using a foreach loop to add each array as an indexed array however this does not work. Any help would be greatly appreciated! :) – Nicholas Mordecai Nov 18 '14 at 01:58
  • @NicholasMordecai what do you mean by dynamic? If you use a 3d, 4d or 5d array, it in the end may be reduced to number of simple arrays. – Kevin Kopf Nov 18 '14 at 04:23
  • @NicholasMordecai Tell me first, is your code really that complex that you have 3d, 4d and so on arrays? Or maybe it needs refactoring? I'm asking because things will get very very hard in that stage :) – Kevin Kopf Nov 19 '14 at 19:36
  • It "should" always be 2d however I do not know how many indexes there will be in that sometimes I may pass $total[0],$total[1] and other times I may need to pass $total[0],$total[1],$total[2],$total[3]. The issue however, is the cartesain function will not accept assosiative arrays but only indexed arrays so array(1,2,3) will work, but array[0] will not. Any help will be greatly appreciated. Thanks, Nick – Nicholas Mordecai Nov 20 '14 at 03:19
  • Nevermind, I fixed the issue. There were no curly braces around the foreach $a as $v. This now allows multi-dimensional arrays from my initial testing. Will mark as correct answer, please feel free to edit if you see fit. Kind Regards, Nick – Nicholas Mordecai Nov 20 '14 at 16:39