WordPress: Menü Einträge als Array zurückgeben

function getNavigationItemsArray(array $menuItems)
{
    $navigationItems = [];
    $parentPackages = [];

    foreach ($menuItems as $menuItem) {
        if ($menuItem->menu_item_parent == 0) {
            continue;
        }

        if (!isset($parentPackages[$menuItem->menu_item_parent])) {
            $parentPackages[$menuItem->menu_item_parent] = [$menuItem];
        } else {
            $parentPackages[$menuItem->menu_item_parent][] = $menuItem;
        }
    }

    foreach ($menuItems as $menuItem) {
        if (isset($parentPackages[$menuItem->ID])) {
            $menuItem->children = $parentPackages[$menuItem->ID];
        } else {
            $menuItem->children = [];
        }

        if ($menuItem->menu_item_parent == 0) {
            $navigationItems[] = $menuItem;
        }
    }

    return $navigationItems;
}