1
$data = array(
'a' => array('a1', 'a2', 'a3'),
'b' => array('b1', 'b2', 'b3', 'b4'),
'c' => array('c1', 'c2', 'c3', 'c4', 'c5'));

to get

a1
a2
a3
b1
b2
b3
b4
c1
c2
c3
c4
c5

a1 b1
a1 b2
a1 b3
a1 b4
a1 c1
a1 c2
a1 c3
a1 c4
a1 c5

b1 c1
b1 c2
b1 c3
b1 c4
b1 c5
b2 c1
b2 c2
b2 c3
b2 c4
b2 c5
b3 c1
b3 c2
b3 c3
b3 c4
b3 c5
b4 c1
b4 c2
b4 c3
b4 c4
b4 c5

a1 b1 c1
a1 b1 c2
a1 b1 c3
a1 b1 c4
a1 b1 c5
a1 b2 c1
a1 b2 c2
a1 b2 c3
a1 b2 c4
a1 b2 c5
a1 b3 c1
a1 b3 c2
a1 b3 c3
a1 b3 c4
a1 b3 c5
a1 b4 c1
a1 b4 c2
a1 b4 c3
a1 b4 c4
a1 b4 c5
etc...

Thanks

troelskn
  • 115,121
  • 27
  • 131
  • 155
DenverZ
  • 49
  • 1
  • 4
  • 1
    This question is even less clear than your recent Perl question, which is VERY similar to this one. You should really elaborate a bit on what it is you want to do! – canavanin Dec 28 '10 at 21:55
  • 1
    Why are you asking the same question for two languages? Kind-of-a-dupe: http://stackoverflow.com/questions/4549529/perl-all-possible-combinations-of-hash – jwueller Dec 28 '10 at 21:56
  • This should work as a starting point: http://stackoverflow.com/questions/2516599/php-2d-array-output-all-combinations – sberry Dec 28 '10 at 21:59
  • It's called "permutations" - Makes it easier to google for. See: http://stereofrog.com/blok/on/070816 – troelskn Dec 28 '10 at 22:13

2 Answers2

5

Apparently you want to build the cartesian product of several arrays, i.e. every element combined with each other element.

In addition, you want to have result tuples that omit one or more of those arrays which, for the sake of simplicity, I would model as having a null element in each of those arrays:

$result = array(array()); // We need to start with one element already, because thats the identity element of the cartesian product
foreach ($data as $arr)
{
    array_push($arr,null); // Add a null element to the array to get tuples with less than all arrays

    // This is the cartesian product:
    $new_result = array();
    foreach ($result as $old_element)
        foreach ($arr as $el)
            $new_result []= array_merge($old_element,array($el));
    $result = $new_result;
}

Note that for your result line a1 b3 c2 this code gives you array('a1','b3','c2') and for your result line b4 c3 this code gives you array('b4','c3',null).

AndreKR
  • 32,613
  • 18
  • 106
  • 168
1

If you want to print them all out, just use loops:

foreach($data['a'] as $k1 =>$v1){
    $output[]=$v1;
    foreach($data['b'] as $k2 => $v2){
        $output[]=$v2;
        $output[]=$v1."-".$v2;
        foreach($data['c'] as $k3 => $v3){
            $output[]=$v3;
            $output[]=$v1."-".$v2."-".$v3;
        }
    }
} 

http://www.webdeveloper.com/forum/showthread.php?t=168409

Google is awesome that way...

If you want to see how many possibilities there are, multiply them:

$count1=1;
$count2=1;
for each $data as $item{
$count2*=count($item);
}
J V
  • 11,402
  • 10
  • 52
  • 72
  • Any way of doing this using a recursive function? – Oli Dec 28 '10 at 22:09
  • @Oli: I could do it in python but I never worked with objects in php (It's much better as a procedural language) Also of note, I missed a line there... you'd need one more for `$v2."-".$v3` so I guess it would be very hard to do in a recursive function (Hence pythons inbuilt `list.product()` function) – J V Dec 29 '10 at 20:18