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

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! Commented Dec 28, 2010 at 21:55
  • 1
    Why are you asking the same question for two languages? Kind-of-a-dupe: stackoverflow.com/questions/4549529/… Commented Dec 28, 2010 at 21:56
  • This should work as a starting point: stackoverflow.com/questions/2516599/… Commented Dec 28, 2010 at 21:59
  • It's called "permutations" - Makes it easier to google for. See: stereofrog.com/blok/on/070816 Commented Dec 28, 2010 at 22:13

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Appreciated, this algorithm also save my day @AndreKR from March :D
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);
}

2 Comments

Any way of doing this using a recursive function?
@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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.