按 parent / 递归排序数组

Sorting array by parent / recurse

我 SQL 查询 returns:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [parent] => 
            [created_at] => 2015
            [updated_at] => 0
            [name] => Strona Główna
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
        )

    [1] => stdClass Object
        (
            [id] => 2
            [parent] => 1
            [created_at] => 2015
            [updated_at] => 0
            [name] => Podstrona strony głównej
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
        )

    [2] => stdClass Object
        (
            [id] => 3
            [parent] => 
            [created_at] => 2015
            [updated_at] => 2015
            [name] => O nas
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
        )

    [3] => stdClass Object
        (
            [id] => 5
            [parent] => 1
            [created_at] => 2015
            [updated_at] => 2015
            [name] => Kolejna podstrona
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
        )

    [4] => stdClass Object
        (
            [id] => 6
            [parent] => 3
            [created_at] => 2015
            [updated_at] => 0
            [name] => Kolejna podstrona
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
        )

    [5] => stdClass Object
        (
            [id] => 8
            [parent] => 5
            [created_at] => 2015
            [updated_at] => 0
            [name] => tuytuytuyt
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
        )

    [6] => stdClass Object
        (
            [id] => 9
            [parent] => 5
            [created_at] => 2015
            [updated_at] => 0
            [name] => fghfhgfh
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
        )

)

而且我几乎成功地进行了排序。如果元素有 parent,它会创建一个数组并将 children 放入该元素,但最后一个元素有问题,我不知道为什么。

现在输出如下所示:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [parent] => 
            [created_at] => 2015
            [updated_at] => 0
            [name] => Strona Główna
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
            [node] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 2
                            [parent] => 1
                            [created_at] => 2015
                            [updated_at] => 0
                            [name] => Podstrona strony głównej
                            [short] => 
                            [content] => 
                            [header] => 
                            [img] => 
                            [visible] => 0
                            [position] => 1000
                            [top] => 0
                            [left] => 0
                            [footer] => 0
                            [dropdown] => 0
                            [chilldren] => 0
                            [title] => 
                            [description] => 
                            [keywords] => 
                            [tags] => 
                            [redirect] => 
                            [js] => 
                            [css] => 
                            [css_class] => 
                            [module] => 
                            [lang] => 0
                            [node] => Array
                                (
                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 5
                            [parent] => 1
                            [created_at] => 2015
                            [updated_at] => 2015
                            [name] => Kolejna podstrona
                            [short] => 
                            [content] => 
                            [header] => 
                            [img] => 
                            [visible] => 0
                            [position] => 1000
                            [top] => 0
                            [left] => 0
                            [footer] => 0
                            [dropdown] => 0
                            [chilldren] => 0
                            [title] => 
                            [description] => 
                            [keywords] => 
                            [tags] => 
                            [redirect] => 
                            [js] => 
                            [css] => 
                            [css_class] => 
                            [module] => 
                            [lang] => 0
                            [node] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 8
                                            [parent] => 5
                                            [created_at] => 2015
                                            [updated_at] => 0
                                            [name] => tuytuytuyt
                                            [short] => 
                                            [content] => 
                                            [header] => 
                                            [img] => 
                                            [visible] => 0
                                            [position] => 1000
                                            [top] => 0
                                            [left] => 0
                                            [footer] => 0
                                            [dropdown] => 0
                                            [chilldren] => 0
                                            [title] => 
                                            [description] => 
                                            [keywords] => 
                                            [tags] => 
                                            [redirect] => 
                                            [js] => 
                                            [css] => 
                                            [css_class] => 
                                            [module] => 
                                            [lang] => 0
                                            [node] => Array
                                                (
                                                )

                                        )

                                    [1] => stdClass Object
                                        (
                                            [id] => 9
                                            [parent] => 5
                                            [created_at] => 2015
                                            [updated_at] => 0
                                            [name] => fghfhgfh
                                            [short] => 
                                            [content] => 
                                            [header] => 
                                            [img] => 
                                            [visible] => 0
                                            [position] => 1000
                                            [top] => 0
                                            [left] => 0
                                            [footer] => 0
                                            [dropdown] => 0
                                            [chilldren] => 0
                                            [title] => 
                                            [description] => 
                                            [keywords] => 
                                            [tags] => 
                                            [redirect] => 
                                            [js] => 
                                            [css] => 
                                            [css_class] => 
                                            [module] => 
                                            [lang] => 0
                                            [node] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => stdClass Object
        (
            [id] => 2
            [parent] => 1
            [created_at] => 2015
            [updated_at] => 0
            [name] => Podstrona strony głównej
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
            [node] => Array
                (
                )

        )

    [2] => stdClass Object
        (
            [id] => 3
            [parent] => 
            [created_at] => 2015
            [updated_at] => 2015
            [name] => O nas
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
            [node] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 6
                            [parent] => 3
                            [created_at] => 2015
                            [updated_at] => 0
                            [name] => Kolejna podstrona
                            [short] => 
                            [content] => 
                            [header] => 
                            [img] => 
                            [visible] => 0
                            [position] => 1000
                            [top] => 0
                            [left] => 0
                            [footer] => 0
                            [dropdown] => 0
                            [chilldren] => 0
                            [title] => 
                            [description] => 
                            [keywords] => 
                            [tags] => 
                            [redirect] => 
                            [js] => 
                            [css] => 
                            [css_class] => 
                            [module] => 
                            [lang] => 0
                            [node] => Array
                                (
                                )

                        )

                )

        )

    [3] => stdClass Object
        (
            [id] => 5
            [parent] => 1
            [created_at] => 2015
            [updated_at] => 2015
            [name] => Kolejna podstrona
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
            [node] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 8
                            [parent] => 5
                            [created_at] => 2015
                            [updated_at] => 0
                            [name] => tuytuytuyt
                            [short] => 
                            [content] => 
                            [header] => 
                            [img] => 
                            [visible] => 0
                            [position] => 1000
                            [top] => 0
                            [left] => 0
                            [footer] => 0
                            [dropdown] => 0
                            [chilldren] => 0
                            [title] => 
                            [description] => 
                            [keywords] => 
                            [tags] => 
                            [redirect] => 
                            [js] => 
                            [css] => 
                            [css_class] => 
                            [module] => 
                            [lang] => 0
                            [node] => Array
                                (
                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 9
                            [parent] => 5
                            [created_at] => 2015
                            [updated_at] => 0
                            [name] => fghfhgfh
                            [short] => 
                            [content] => 
                            [header] => 
                            [img] => 
                            [visible] => 0
                            [position] => 1000
                            [top] => 0
                            [left] => 0
                            [footer] => 0
                            [dropdown] => 0
                            [chilldren] => 0
                            [title] => 
                            [description] => 
                            [keywords] => 
                            [tags] => 
                            [redirect] => 
                            [js] => 
                            [css] => 
                            [css_class] => 
                            [module] => 
                            [lang] => 0
                            [node] => Array
                                (
                                )

                        )

                )

        )

    [4] => stdClass Object
        (
            [id] => 6
            [parent] => 3
            [created_at] => 2015
            [updated_at] => 0
            [name] => Kolejna podstrona
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
            [node] => Array
                (
                )

        )

    [5] => stdClass Object
        (
            [id] => 8
            [parent] => 5
            [created_at] => 2015
            [updated_at] => 0
            [name] => tuytuytuyt
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
            [node] => Array
                (
                )

        )

    [6] => stdClass Object
        (
            [id] => 9
            [parent] => 5
            [created_at] => 2015
            [updated_at] => 0
            [name] => fghfhgfh
            [short] => 
            [content] => 
            [header] => 
            [img] => 
            [visible] => 0
            [position] => 1000
            [top] => 0
            [left] => 0
            [footer] => 0
            [dropdown] => 0
            [chilldren] => 0
            [title] => 
            [description] => 
            [keywords] => 
            [tags] => 
            [redirect] => 
            [js] => 
            [css] => 
            [css_class] => 
            [module] => 
            [lang] => 0
            [node] => Array
                (
                )

        )

)

如您所见,最后一个元素未被过滤。

"sorting" parent 的代码如下所示:

            $new = [];
            foreach($data as $key => $item) {
              $data[$key] - > node = [];
              array_push($new, $data[$key]);

              for ($i = 0; $i < count($new); $i++) {
                if ($new[$i] - > id == $data[$key] - > parent) {
                  array_push($new[$i] - > node, $data[$key]);
                }

              }
            }

            print_r($data);

任何人都可以向我解释我做错了什么吗?

当你遍历结果时,你应该询问当前元素是否有父元素。如果元素没有父元素,则简单地将元素添加到新数组中,但如果元素有父元素,则将其添加到该元素的节点列表中:

$new = [];
foreach($data as $key => $item) {    
  if(!empty($item->parent)) 
    $new[$item->id]->node[]= $item;
  else
    $new[$item->id]= $item;
}
print_r($data);

当然,只有当元素的父元素在结果集中出现在该元素之前时,这才会起作用。

您可以使用递归函数,如下所示:

function makeTree($data, $parent) {
    $tree = [];
    foreach($data as $item) {
        if ($item -> parent == $parent) {
            $item -> node = makeTree($data, $item -> id);
            $tree[] = $item;
        }
    }
    return $tree;
}

$new = makeTree($data, null);

print_r($new);

此函数创建了一个包含给定父项的所有后代的树。当一个元素没有子元素时递归停止,然后函数 return 是一个空数组。

调用者使用函数(数组)的return值,将其添加到父节点的node 属性。

该函数首先由根值调用,即在没有父项时分配给 parent 属性 的值。在您的情况下,这是 null,所以这就是主调用将 null 作为第二个参数的原因。