php数组合并递归与相同的数组值并创建组合

时间:2017-11-06 18:16:22

标签: php arrays recursion combinations

您好我是php的新手,我只是陷入了头脑风暴阵列,我有以下的php数组

[0] => Array
    (
        [0] => Array
            (
                [1] => 5
                [2] => 12
                [11] => 15
            )

        [1] => Array
            (
                [1] => 5
                [2] => 12
                [11] => 16
            )

        [2] => Array
            (
                [1] => 4
                [2] => 9
                [11] => 15
            )

        [3] => Array
            (
                [1] => 3
                [2] => 9
                [11] => 15
            )

        [4] => Array
            (
                [1] => 3
                [2] => 9
                [11] => 16
            )

        [5] => Array
            (
                [1] => 3
                [2] => 13
                [11] => 15
            )

        [6] => Array
            (
                [1] => 3
                [2] => 13
                [11] => 16
            )

        [7] => Array
            (
                [1] => 3
                [2] => 12
                [11] => 15
            )

        [8] => Array
            (
                [1] => 3
                [2] => 12
                [11] => 16
            )

        [9] => Array
            (
                [1] => 4
                [2] => 9
                [11] => 16
            )

        [10] => Array
            (
                [1] => 4
                [2] => 13
                [11] => 15
            )

        [11] => Array
            (
                [1] => 4
                [2] => 13
                [11] => 16
            )

        [12] => Array
            (
                [1] => 4
                [2] => 12
                [11] => 15
            )

        [13] => Array
            (
                [1] => 4
                [2] => 12
                [11] => 16
            )

        [14] => Array
            (
                [1] => 5
                [2] => 9
                [11] => 15
            )

        [15] => Array
            (
                [1] => 5
                [2] => 13
                [11] => 16
            )

    )

我想要低于标准

[0]=> Array
(
    [5]=> Array
    (
        [12]=> Array
        (
            [0] => 15
            [1] => 16
        )
        [9]=> Array
        (
            [0] => 15
        )
        [13]=> Array
        (
            [0] => 16
        )
    )
    [4]=> Array
    (
        [9]=> Array
        (
            [0] => 15
            [1] => 16
        )
        [13]=> Array
        (
            [0] => 16
        )
        [12]=> Array
        (
            [0] => 15
            [1] => 16
        )
    )
    [3]=> Array
    (
        [9]=> Array
        (
            [0] => 15
            [1] => 16 
        )
        [13]=> Array
        (
            [0] => 15
            [1] => 16
        )
        [12]=> Array
        (
            [0] => 15
            [1] => 16

        )
    )
)

很抱歉出来不太清楚但是我想检查每个数组的第一个常见值,例如5,4和3然后基于这些值遍历每个值并创建树数组 作为示例简单我希望每个数组第一个元素,然后递归地数组检查值并创建新的 示例只有3级但我想要递归的n级,并且帮助将是Appreciate。

2 个答案:

答案 0 :(得分:1)

不确定我是否理解正确,因为样品没有清楚解释但我能以某种方式打印出所需的输出。通过评论给出的解释

// I just simplified your given array
$input[0] = array(
    [1 => 5, 2 => 12, 11 => 15],
    [1 => 5, 2 => 12, 11 => 16],
    [1 => 4, 2 =>  9, 11 => 15],
    [1 => 3, 2 =>  9, 11 => 15],
    [1 => 3, 2 =>  9, 11 => 16],
    [1 => 3, 2 => 13, 11 => 15],
    [1 => 3, 2 => 13, 11 => 16],
    [1 => 3, 2 => 12, 11 => 15],
    [1 => 3, 2 => 12, 11 => 16],
    [1 => 4, 2 =>  9, 11 => 16],
    [1 => 4, 2 => 13, 11 => 15],
    [1 => 4, 2 => 13, 11 => 16],
    [1 => 4, 2 => 12, 11 => 15],
    [1 => 4, 2 => 12, 11 => 16],
    [1 => 5, 2 =>  9, 11 => 15],
    [1 => 5, 2 => 13, 11 => 16]
);

// Declare new output array
$output[0] = [];

// Loop thru each array inside $input[0] array
foreach($input[0] as $key => $value) {

    // Set each array value to variables
    $one = $value[1];
    $two = $value[2];
    $eleven = $value[11];

    // Check if $one key already exist, create if not
    if (!isset($output[0][$one])) {
            $output[0][$one] = [];
    }

    // Check if $two key already exist, create if not
    if (!isset($output[$one][$two])) {
            $output[0][$one][$two] = [];
    }

    // push each $eleven
    $output[0][$one][$two][] = $eleven;
};

// Prints output array
echo '<pre>';
print_r($output);

答案 1 :(得分:0)

为了适应您有多个第一级元素的情况,请使用循环。使用另一个循环来迭代每个条目。您甚至可以在嵌套的 foreach() 中使用数组解构来声明变量。

代码:(Demo)

$result = [];
foreach ($input as $key => $array) {
    foreach ($array as [1 => $one, 2 => $two, 11 => $eleven]) {
        $result[$key][$one][$two][] = $eleven;
    }
}
var_export($result);

无需声明/分配父键即可将数据附加到其中。我的代码段中故意没有条件。