duangou1953 2015-02-23 21:36
浏览 28
已采纳

递归PHP树(排列)

I'm looking to write a function which creates all permutation of a list of arrays (The list is dynamical). Now I found 2 articles, http://dannyherran.com/2011/06/finding-unique-array-combinations-with-php-permutations/ and Finding cartesian product with PHP associative arrays. But I don't want to store them as multiple arrays, I want to add each array to each possibility so I can use them later.

In fact I want to multiply each array with the other.

For example:

$array = array(
    array(
        1,
        2
        ),
    array(
        'A',
        'B',
        'C'),
    array(
        'I',
        'II')
    );

In this form:

Array
(
[0] => Array
    (
        [0] => 1
        [1] => Array
            (
                [0] => Array
                    (
                        [0] => A
                        [1] => Array
                            (
                                [0] => I
                                [1] => II
                            )
                    )
                [1] => Array
                    (
                        [0] => B
                        [1] => Array
                            (
                                [0] => I
                                [1] => II
                            )
                    )
                [2] => Array
                    (
                        [0] => C
                        [1] => Array
                            (
                                [0] => I
                                [1] => II
                            )
                    )
            )
    )
[1] => Array
    (
        [0] => 2
        [1] => Array
            (
                [0] => Array
                    (
                        [0] => A
                        [1] => Array
                            (
                                [0] => I
                                [1] => II
                            )
                    )
                [1] => Array
                    (
                        [0] => B
                        [1] => Array
                            (
                                [0] => I
                                [1] => II
                            )
                    )
                [2] => Array
                    (
                        [0] => C
                        [1] => Array
                            (
                                [0] => I
                                [1] => II
                            )
                    )
            )
    )
)

I think this big example made my problem clear. For this type of array I created a function: foreach ($array[1] as $value) { $return1[] = array($value, $array[2]); }

foreach ($array[0] as $value) {
    $return[] = array($value, $return1);
}

print_r($return);

Now I want to create this function inside a recursive function (so it becomes dynamical) but I got stuck. I wanted to pass the amount of arrays to the function and then iterate.

function createTree($array, $loops=3){

$b = $array[$loops-2];

foreach ($b as $v) {
    $return[] = array($v, createTree($return, $loops-1));
}
print_r($return);
}

Maybe there is a total other solution to multiply the arrays? But the function which isn't recursive is easy for me, but making it recursive...

Thanks for your help

  • 写回答

1条回答 默认 最新

  • dso0139 2015-02-23 22:36
    关注
    function createTree($array){
        switch(count($array)) {
        case 0:
            die('Illegal argument.');
        case 1:
            return $array[0];
        default:
            $lastArray = array_pop($array);
    
            $subArray = createTree($array);
    
            foreach ($lastArray as $item) {
                $return[] = array($item, $subArray);
            }
    
            return $return;
        }
    }
    
    var_dump(createTree(array_reverse($array)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型