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

  • SEARCH
  • Home
  • 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 8919663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:05:09+00:00 2026-06-15T06:05:09+00:00

I have a flat array full of navigation menu items in WP. Each item

  • 0

I have a flat array full of navigation menu items in WP. Each item knows its parent, but none of them seem to know if they have children.

            Array
(
    [0] => stdClass Object
        (
            [ID] => 22
            [menu_item_parent] => 0
        )
    [1] => stdClass Object
        (
            [ID] => 108
            [menu_item_parent] => 22
        )
    [2] => stdClass Object
        (
            [ID] => 117
            [menu_item_parent] => 108
        )
    [3] => stdClass Object
        (
            [ID] => 118
            [menu_item_parent] => 108
        )
    [4] => stdClass Object
        (
            [ID] => 106
            [menu_item_parent] => 22
        )
    [5] => stdClass Object
        (
            [ID] => 119
            [menu_item_parent] => 106
        )
    [6] => stdClass Object
        (
            [ID] => 120
            [menu_item_parent] => 106
        )
    [7] => stdClass Object
        (
            [ID] => 23
            [menu_item_parent] => 0
        )
)

I’ve tried approaching it with conditional logic a few different ways (and each time hard-coding values which isn’t ideal), but I keep coding myself into a mess. How can I iterate over the array to produce a result like this?

<ul>
    <li>22
        <ul class="child">
            <li>108
                <ul class="grandchild">
                    <li>117</li>
                    <li>118</li>
                </ul>
            </li>
            <li>106
                <ul class="grandchild">
                    <li>119</li>
                    <li>120</li>
                </ul>
            </li>           
        </ul>
    </li>
    <li>23</li>
</ul>
  • 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-06-15T06:05:10+00:00Added an answer on June 15, 2026 at 6:05 am

    I ended up doing it like this:

    <?php 
    $items = wp_get_nav_menu_items('primary'); // original flat array
    foreach ($items as $item) {
        $pages[$item->menu_item_parent][] = $item; // create new array
    }
    foreach ($pages[0] as $parent) {
        if (isset($pages[$parent->ID])) { // if page has children
            echo '<li>'.$parent->ID;
            echo '<ul class="children">';
            foreach ($pages[$parent->ID] as $child) {
                if (isset($pages[$child->ID])) { // child has children
                    echo '<li>'.$child->ID;
                    echo '<ul class="grandchildren">';
                    foreach ($pages[$child->ID] as $grandchild) {
                        echo '<li>'.$grandchild->ID.'</li>';
                    }
                    echo '</ul></li>';
                } else {
                    echo '<li>'.$child->ID.'</li>'; // child has no children
                }
            }
            echo '</ul></li>';
        } else { // page has no children
            echo '<li>'.$parent->ID.'</li>';
        }
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set of flat files (114 files) each file is named with database
I have several hundred files in a non-flat directory structure. My Makefile lists each
I have a flat array that I'm trying to make multidimensional. Basically, I want
PROBLEM I have a nested PHP array that I need to populate from flat
I have a flat array of pipe-delimited strings: Array ( [0] => style1|000000 [1]
I have a big 2-dimensional array A , and also a flat array B
I have a flat associative array which may contain duplicate values. Array ( [for-juniors]
I have an array that is read from a flat file that looks like
I'm trying to migrate my flat php project to Symfony2, but its coming to
I have a collection of keys in this massive flat single array I would

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.