Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Created January 25, 2016 12:59
Show Gist options
  • Save kirilkirkov/2a06fa2d459d397edb0a to your computer and use it in GitHub Desktop.
Save kirilkirkov/2a06fa2d459d397edb0a to your computer and use it in GitHub Desktop.
Build Tree Structure from array
public function getPagesForSpace() {
$arr = array();
$result = $this->query("SELECT * FROM pages ORDER BY id ASC");
if($result !== false) {
while ($row = $result->fetch_assoc()) {
$arr[] = $row;
}
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if($element['sub_for'] == $parentId) {
$children = buildTree($elements, $element['id']);
if($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
return $tree = buildTree($arr);
}
}
@kirilkirkov
Copy link
Author

This simple example will crate something like this:
When table structure is
id <-- parent_id

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => Space 1
            [content] => 
            [sub_for] => 0
            [for_space] => 1
            [category] => 0
            [created] => 1453716270
            [created_from] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [title] => Sub page 1
                            [content] => 
                            [sub_for] => 1
                            [for_space] => 1
                            [category] => 0
                            [created] => 1453716312
                            [created_from] => 1
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [title] => Sub Page 2
                            [content] => 
                            [sub_for] => 1
                            [for_space] => 1
                            [category] => 0
                            [created] => 1453716322
                            [created_from] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [title] => Sub Page 3
                                            [content] => 
                                            [sub_for] => 3
                                            [for_space] => 1
                                            [category] => 0
                                            [created] => 1453716335
                                            [created_from] => 1
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 5
            [title] => Space 2
            [content] => 
            [sub_for] => 0
            [for_space] => 1
            [category] => 0
            [created] => 1453716354
            [created_from] => 1
        )

)
``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment