Create array of all pages (Multidimensional)

Hi struggling with this. Was able to get a recursive function to do it with a list.
Now how do I get an multidimensional array of all pages, similar to the nested list.

$children = $site->children()->not('home', 'error', $page);
        
        function doUL($children) {
            if ($children) {
                echo '<ul>';
                foreach ($children as $key => $child) {
                    echo '<li>' . $child->title() .'</li>';

                    if ($child->hasChildren()) {
                        doUL($child->children());
                    }

                }
                echo '</ul>';
            }
        }

        doUL($children);

1 Like

And there you go…

$children = $site->index()->not('home', 'error', $page);

$indexData = array();

foreach ($children as $child) {
    $indexData[] = [
        'page' => (string)$child->uid(),
        'parent_page' => (string)$child->parent()->uid()
    ];
}

function buildTree($elements, $parentId) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element[parent_page] == $parentId) {

            $children = buildTree($elements, $element[page]);

            if ($children) {
                $element['children'] = $children;
            }

            $branch[] = $element;
        }
    }

    return $branch;
}

$tree = buildTree($indexData);

dump($tree);