Search code examples
phparrayssortingparent

Sorting array by parent / recurse


I have SQL query that 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
        )

)

And I've almost successfully made it to sort. If element has a parent, it creates an array and put children to this element, but there's a problem with last elements and I dunno why.

Now output looks like this:

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

        )

)

As you can see last elements are not filtered.

Code for "sorting" parent's looks like:

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

Can anyone explain to me what am I doing wrong?


Solution

  • You can use a recursive function, like this:

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

    This function makes a tree of all descendants of the given parent. The recursion stops when an element has no children, and then the function returns an empty array.

    The return value of the function (an array) is used by the caller, to add it to the node property of the parent node.

    The function is called first by the root value, i.e. the value you assign to the parent property when there is no parent. In your case is this is null, so that is why the main call has null as second argument.