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

时间:2018-01-03 19:28:47

标签: php arrays multidimensional-array nested

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

  
      
  1. 接受嵌套数组树
  2.   
  3. 在我的情况下指定女巫子阵列变平我有['孩子']用父物品压扁
  4.   

当前树:

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个以上的嵌套行要处理,最好的表现。

1 个答案:

答案 0 :(得分:2)

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

  1. 对于数组中的每个对象,将其子项分开,然后将其添加到输出数组中。
  2. 以递归方式展平子数组,并将结果添加到输出数组中。
  3. 这是一个以这种方式运作的例子。

    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顺序”,但如果你的意思是我认为你做的,那就可以做到。