按父/递归排序数组

时间:2015-12-03 18:47:22

标签: php arrays sorting parent

我有SQL查询返回:

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
        )

)

我几乎成功地将其排序。如果element有一个父元素,它会创建一个数组并将子元素放到这个元素中,但是最后一个元素存在问题,我不知道为什么。

现在输出如下:

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
                (
                )

        )

)

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

“排序”父母的代码如下:

            $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);

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

2 个答案:

答案 0 :(得分:0)

当您迭代结果时,您应该询问当前元素是否具有父元素。如果元素没有父元素,则只需将元素添加到新数组中,但如果元素具有父元素,则将其添加到该元素的节点列表中:

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

当然,只有当元素的父元素出现在结果集中的元素之前时,这才有效。

答案 1 :(得分:0)

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

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);

此函数生成给定父级的所有后代的树。当元素没有子元素时递归停止,然后函数返回一个空数组。

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

该函数首先由根值调用,即在没有父值时分配给parent属性的值。在你的情况下,这是null,这就是为什么主调用null作为第二个参数。