Foreach循环+递归

I am trying to put this code in a more flexible manner so it can work whatever the size of $sets array is. I suppose it can be done with recursion but cannot find the correct php syntax.

$sets = array(
                array(0, 1, 2, 3),
                array(0, 1, 2, 3),
                array(0, 1, 2, 3),
                array(0, 1, 2, 3)
                );

$combinations = array();

foreach($sets[0] as $s1)
    foreach($sets[1] as $s2)
        foreach($sets[2] as $s3)
            foreach($sets[3] as $s4)
                $combinations[] = array($s1, $s2, $s3, $s4);

print_r($combinations);
查看全部
doupang4126
doupang4126
2014/04/28 11:52
  • foreach
  • php
  • recursion
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复