如何生成排列和组合结果

时间:2013-03-13 10:22:51

标签: php arrays multidimensional-array

我有这个数组

$dataArray = array
        (
        array(1,11,111),
        array(2,22,222),
        array(3,33,333),
        array(4,44,444)
    );

我也是谷歌,但找不到有用的PHP脚本..

结果使用排列组合方法

1,11,111
1,111,11
11,111
1,11,
2,11,111
2,11,1
.
.
.
.

是的,我已经用排列组合方法尝试了它

function permutations(array $array, $r=false)
    {
        switch (count($array)) {
            case 1:
                return $array[0];
                break;
        }

        $keys = array_keys($array);

        $a = array_shift($array);
        $k = array_shift($keys); // Get the key that $a had
        $b = permutations($array, 'recursing');

        $return = array();
        foreach ($a as $v) {
            if($v)
            {
                foreach ($b as $v2) {
                    if($r == 'recursing')
                        $return[] = array_merge(array($v), (array) $v2);
                    else
                        $return[] = array($k => $v) + array_combine($keys, $v2);
                }
            }
        }

    return $return;
}


$x = permutations($dataArray);      
echo "<pre>";               
print_r($x);

0 个答案:

没有答案