Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3285306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:15:09+00:00 2026-05-17T20:15:09+00:00

I’m trying to build a navigation menu. I am receiving an array with a

  • 0

I’m trying to build a navigation menu. I am receiving an array with a structure like this:

[ 
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = C
      ]
    ]
  ],
  [
    Title = A
    Sub items = [
      Title = B
      Sub items = [
        Title = D
      ]
    ]
  ],
]

I need to take it and make it look like this:

[
  Title = A
  Sub items = [
    Title = B
    Sub items = [
      Title = C,
      Title = D
    ]
  ]
]

I have listed my input and my desired output below. I’m tearing my hair out on this one. I was trying to use recursion, but had some issues with running into infinite loops all over the place. Any help on a good way to tackle this would be so appreciated.

Input:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                )

        )

    [2] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Routes
                            [path] => modules/routes/settings/routes/
                        )

                )

        )

    [3] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                )

        )

    [4] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Sessions
                            [path] => modules/sessions/settings/sessions/
                        )

                )

        )

    [5] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Databases
                            [path] => modules/databases/settings/databases/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a Database
                                            [path] => modules/databases/settings/databases/add-a-database/
                                        )

                                )

                        )

                )

        )

    [6] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Models
                            [path] => modules/models/settings/models/
                        )

                )

        )

    [7] => Array
        (
            [title] => Settings
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users and Accounts
                            [path] => modules/users-and-accounts/settings/users-and-accounts/
                        )

                )

        )

    [8] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
                                            [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Output:

Array
(
    [0] => Array
        (
            [title] => Dashboard
            [path] => dashboard/
        )

    [1] => Array
        (
            [title] => Settings
            [path] => settings/
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Instances
                            [path] => settings/instances/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Instance
                                            [path] => settings/instances/add-an-instance/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Modules
                            [path] => settings/modules/
                        )

                    [2] => Array
                        (
                            [title] => Administrator
                            [path] => settings/administrator/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Change Password
                                            [path] => settings/administrator/change-password/
                                        )

                                )

                        )

                    [3] => Array
                    (
                        [title] => Routes
                        [path] => modules/routes/settings/routes/
                    )

                    [4] => Array
                        (
                            [title] => Cookies
                            [path] => modules/cookies/settings/cookies/
                        )

                    [5] => Array
                    (
                        [title] => Sessions
                        [path] => modules/sessions/settings/sessions/
                    )

                    [6] => Array
                    (
                        [title] => Databases
                        [path] => modules/databases/settings/databases/
                        [subItems] => Array
                            (
                                [0] => Array
                                    (
                                        [title] => Add a Database
                                        [path] => modules/databases/settings/databases/add-a-database/
                                    )

                            )

                        )

                    [7] => Array
                    (
                        [title] => Models
                        [path] => modules/models/settings/models/
                    )

                    [8] => Array
                    (
                        [title] => Users and Accounts
                        [path] => modules/users-and-accounts/settings/users-and-accounts/
                    )
                )
        )
    [2] => Array
        (
            [title] => Users and Accounts
            [subItems] => Array
                (
                    [0] => Array
                        (
                            [title] => Users
                            [path] => modules/users-and-accounts/users/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add a User
                                            [path] => modules/users-and-accounts/users/add-a-user/
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [title] => Accounts
                            [path] => modules/users-and-accounts/accounts/
                            [subItems] => Array
                                (
                                    [0] => Array
                                        (
                                            [title] => Add an Account
                                            [path] => modules/users-and-accounts/accounts/add-an-account/
                                        )

                                    [1] => Array
                                        (
                                            [title] => Account Types
                                            [path] => modules/users-and-accounts/accounts/account-types/
                                            [subItems] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [title] => Add an Account Type
                                                            [path] => modules/users-and-accounts/accounts/account-types/add-an-account-type/
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:15:09+00:00Added an answer on May 17, 2026 at 8:15 pm
    <?php
    
    function flattenArray($subitems) {
        $titles = Array() ;
        foreach($subitems as $subkey => $subvalue) {
            if(!is_array($subvalue) 
                || !isset($subvalue['title'])
                || !isset($subvalue['subItems'])
                || !is_array($subvalue['subItems'])) {
    
                //Leave this element as-is as it doesn't conform to expectations.
                continue ; 
            }
            if(isset($titles[$subvalue['title']])) {
                foreach($subvalue['subItems'] as $subItem) {
                    $subitems[$titles[$subvalue['title']]]['subItems'][] = $subItem ;
                }
                unset($subitems[$subkey]) ;
            } else {
                $titles[$subvalue['title']] = $subkey ;
            }
        }
        foreach($subitems as $subkey => $subvalue) {
            if(is_array($subvalue) && isset($subvalue['subItems'])) {
                $subitems[$subkey]['subItems'] = flattenArray($subvalue['subItems']) ;
            }
        }
        return $subitems ;
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.