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 8976807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:13:46+00:00 2026-06-15T19:13:46+00:00

I have an array ($nested) like this (this is the print_r() output) Array (

  • 0

I have an array ($nested) like this (this is the print_r() output)

Array
(
    [1] => Array
        (
            [id] => 1
            [module] => 1
            [parent] => 0
            [name] => Audio
        )

    [20] => Array
        (
            [id] => 20
            [module] => 1
            [parent] => 5
            [name] => Mixer analogici
        )

    [16] => Array
        (
            [id] => 16
            [module] => 1
            [parent] => 4
            [name] => Videoproiettori
        )

    [11] => Array
        (
            [id] => 11
            [module] => 1
            [parent] => 2
            [name] => Strutture
        )

    ...

    [19] => Array
        (
            [id] => 19
            [module] => 1
            [parent] => 5
            [name] => Mixer digitali
        )

)

And a recursive function to create a nested menu:

function nmenu($module, $parent, $array) {
    $has_children = false;
    foreach($array as $key => $value) {
        if ($value['module'] == $module) {  
            if ($value['parent'] == $parent) {       
                if ($has_children === false && $parent) {
                    $has_children = true;
                    echo "<ul>\n";
                }
                echo '<li>';
                echo '' . $value['name'] . " \n";
                    nmenu($module, $key, $array);
                echo "</li>\n";
            }
        }
    }
    if ($has_children === true && $parent) echo "</ul>\n";
}

That I call like this:

<ul>
    <?php nmenu($row_rsNavModules['mod_id'], 0, $nested) ?>
</ul>

All is fine, the recursive function works as expected and it creates a series of infinite nested ULs… well, it’s the “infinite” that it’s too much: I’d like to limit the indentation level (aka the number of indented ULS) to 2, like this:

Cat 1
    Subcat 1-1
    Subcat 1-2
    ...
Cat 2
    Subcat 2-1
    Subcat 2-2
    ...

And NOT ending up like this:

Cat 1
    Subcat 1-1
            Sub-Subcat 1-1-1
                    Sub-Sub-Subcat 1-1-1-1
                    ...

Please, how can I edit the previous function to achieve this?
Thanks in advance!

  • 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-15T19:13:47+00:00Added an answer on June 15, 2026 at 7:13 pm

    You only need to have a counter and quit from the function once the counter reaches your limit. Something like:

    //counter parameter added
    function nmenu($module, $parent, $array, $counter) {
        $has_children = false;
        foreach($array as $key => $value) {
            if ($value['module'] == $module) {  
                if ($value['parent'] == $parent) {       
                    if ($has_children === false && $parent) {
                        $has_children = true;
                        echo "<ul>\n";
                    }
                    echo '<li>';
                    echo '' . $value['name'] . " \n";
    
                    if($counter<2){  //added 
                        //counter parameter added
                        nmenu($module, $key, $array, $counter+1);
                    } //added
                    echo "</li>\n";
                }
            }
        }
        if ($has_children === true && $parent) echo "</ul>\n";
    }
    

    And call it like this:

    <ul>
        <?php 
           //counter parameter added 
           nmenu($row_rsNavModules['mod_id'], 0, $nested, 1) 
        ?>
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nested associative array like this: $inputTypes= array( natural => array( text,
I have a hierarchically nested associative array. It looks like this: A = {
I have a array with nested dictionary data like this: [{ link : [
I have a nested array (only one level deep) like this: $a = array(
I have an array (a nested array to be more precise), like this: [[Armando,
I have a multi-dimension array in php like this $shop = array( array(name,point,number), array('Ranjit',
I have nested array in javascript like this: testArray['element1'] = {par1: value1, par2: value2
I have a nested array like this: array = [ { id: 67, sub:
I have an array with nested arrays that looks like this: var tw =
I have an array which looks like this $dataArray = array ( 0 =>

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.