具有特定子数组的PHP嵌套数组函数,以便与父项一起展平

问题描述 投票:0回答:1

功能/功能需要执行以下操作:

  1. 接受嵌套数组树
  2. 在我的情况下指定女巫子阵列变平我有['孩子']与父项平坦

当前树:

Array
(
    [0] => stdClass Object
    (
        [id] => 1
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
            [0] => stdClass Object
            (
                [id] => 2
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 1
                [message] => Lorem ipsum
                [created_at] => 2018-01-03 04:21:49
                [children] => Array
                (
                )

            )

            [1] => stdClass Object
            (
                [id] => 247
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 1
                [message] => 
                [created_at] => 2018-01-03 04:30:07
                [children] => Array
                (
                )

            )

        )

    )

    [1] => stdClass Object
    (
        [id] => 3
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
            [0] => stdClass Object
            (
                [id] => 4
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 3
                [message] => Lorem ipsum
                [created_at] => 2018-01-03 04:21:49
                [children] => Array
                (
                    [0] => stdClass Object
                    (
                        [id] => 250
                        [comment_threads_id] => 1
                        [users_id] => 2
                        [parent_id] => 4
                        [message] => 
                        [created_at] => 2018-01-03 05:03:09
                        [children] => Array
                        (
                        )

                    )

                    [1] => stdClass Object
                    (
                        [id] => 251
                        [comment_threads_id] => 1
                        [users_id] => 2
                        [parent_id] => 4
                        [message] => 
                        [created_at] => 2018-01-03 05:25:58
                        [children] => Array
                        (
                            [0] => stdClass Object
                            (
                                [id] => 252
                                [comment_threads_id] => 1
                                [users_id] => 2
                                [parent_id] => 251
                                [message] => 
                                [created_at] => 2018-01-03 05:05:09
                                [children] => Array
                                (
                                )

                            )
                        )

                    )

                )

            )

            [1] => stdClass Object
            (
                [id] => 248
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 3
                [message] => 
                [created_at] => 2018-01-03 05:02:05
                [children] => Array
                (
                )

            )

        )

    )

    [2] => stdClass Object
    (
        [id] => 5
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
            [0] => stdClass Object
            (
                [id] => 6
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 5
                [message] => Lorem ipsum
                [created_at] => 2018-01-03 04:21:49
                [children] => Array
                (
                    [0] => stdClass Object
                    (
                        [id] => 7
                        [comment_threads_id] => 1
                        [users_id] => 2
                        [parent_id] => 6
                        [message] => Lorem ipsum
                        [created_at] => 2018-01-03 04:21:49
                        [children] => Array
                        (
                            [0] => stdClass Object
                            (
                                [id] => 8
                                [comment_threads_id] => 1
                                [users_id] => 2
                                [parent_id] => 7
                                [message] => Lorem ipsum
                                [created_at] => 2018-01-03 04:21:49
                                [children] => Array
                                (
                                    [0] => stdClass Object
                                    (
                                        [id] => 9
                                        [comment_threads_id] => 1
                                        [users_id] => 1
                                        [parent_id] => 8
                                        [message] => Lorem ipsum
                                        [created_at] => 2018-01-03 04:21:49
                                        [children] => Array
                                        (
                                            [0] => stdClass Object
                                            (
                                                [id] => 10
                                                [comment_threads_id] => 1
                                                [users_id] => 2
                                                [parent_id] => 9
                                                [message] => Lorem ipsum
                                                [created_at] => 2018-01-03 04:21:49
                                                [children] => Array
                                                (
                                                    [0] => stdClass Object
                                                    (
                                                        [id] => 11
                                                        [comment_threads_id] => 1
                                                        [users_id] => 2
                                                        [parent_id] => 10
                                                        [message] => Lorem ipsum
                                                        [created_at] => 2018-01-03 04:21:49
                                                        [children] => Array
                                                        (
                                                            [0] => stdClass Object
                                                            (
                                                                [id] => 12
                                                                [comment_threads_id] => 1
                                                                [users_id] => 2
                                                                [parent_id] => 11
                                                                [message] => Lorem ipsum
                                                                [created_at] => 2018-01-03 04:21:49
                                                                [children] => Array
                                                                (
                                                                    [0] => stdClass Object
                                                                    (
                                                                        [id] => 13
                                                                        [comment_threads_id] => 1
                                                                        [users_id] => 2
                                                                        [parent_id] => 12
                                                                        [message] => Lorem ipsum
                                                                        [created_at] => 2018-01-03 04:21:49
                                                                        [children] => Array
                                                                        (
                                                                        )

                                                                    )

                                                                )

                                                            )

                                                        )

                                                    )

                                                )

                                            )

                                        )

                                    )

                                )

                            )

                        )

                    )

                )

            )

        )

    )

)

它应该是什么样的:

Array
(
    [0] => stdClass Object
    (
        [id] => 1
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [1] => stdClass Object
    (
        [id] => 2
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 1
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [2] => stdClass Object
    (
        [id] => 247
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 1
        [message] => 
        [created_at] => 2018-01-03 04:30:07
        [children] => Array
        (
        )

    )

    [3] => stdClass Object
    (
        [id] => 3
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [4] => stdClass Object
    (
        [id] => 4
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 3
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [5] => stdClass Object
    (
        [id] => 250
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 4
        [message] => 
        [created_at] => 2018-01-03 05:03:09
        [children] => Array
        (
        )

    )

    [6] => stdClass Object
    (
        [id] => 251
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 4
        [message] => 
        [created_at] => 2018-01-03 05:25:58
        [children] => Array
        (           
        )

    )

    [7] => stdClass Object
    (
        [id] => 252
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 251
        [message] => 
        [created_at] => 2018-01-03 05:05:09
        [children] => Array
        (
        )

    )

    [8] => stdClass Object
    (
        [id] => 248
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 3
        [message] => 
        [created_at] => 2018-01-03 05:02:05
        [children] => Array
        (
        )

    )



    [9] => stdClass Object
    (
        [id] => 5
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (           
        )

    )

    [10] => stdClass Object
    (
        [id] => 6
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 5
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [11] => stdClass Object
    (
        [id] => 7
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 6
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [12] => stdClass Object
    (
        [id] => 8
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 7
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [13] => stdClass Object
    (
        [id] => 9
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 8
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [14] => stdClass Object
    (
        [id] => 10
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 9
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [15] => stdClass Object
    (
        [id] => 11
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 10
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [16] => stdClass Object
    (
        [id] => 12
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 11
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [17] => stdClass Object
    (
        [id] => 13
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 12
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

)

因此,对于每个父对象,我需要提取它的子数组(如果它不是空的)和每个子对象(如果它不是空的)我需要提取它的数组,依此类推。

在最终数组中提取时,它需要保持相同的ID顺序(这是目标)。

我尝试了几个类似的方法,使用一些PHP SPL迭代器函数展平数组,但没有它们但我无法得到正确的顺序,在结束数组中有丢失的对象,到目前为止我也希望得到最好的性能这是为了处理10.000和更多的嵌套行。

php arrays multidimensional-array nested
1个回答
2
投票

像这样展平对象数组的递归函数基本上需要做两件事。

  1. 对于数组中的每个对象,将其子项分开,然后将其添加到输出数组中。
  2. 递归地展平子数组并将结果添加到输出数组。

这是一个以这种方式工作的例子。

function flatten($input, $key) {
    $output = [];

    // For each object in the array
    foreach ($input as $object) {

        // separate its children
        $children = isset($object->$key) ? $object->$key : [];
        $object->$key = [];

        // and add it to the output array
        $output[] = $object;

        // Recursively flatten the array of children
        $children = flatten($children, $key);

        //  and add the result to the output array
        foreach ($children as $child) {
            $output[] = $child;
        }
    }
    return $output;
}

我不是100%肯定你的意思是“保持相同的ID顺序”,但如果你的意思是我认为你做的,这将做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.