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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:52:52+00:00 2026-06-18T05:52:52+00:00

I am struggling with an array I want to turn into a nested <

  • 0

I am struggling with an array I want to turn into a nested < select >

I need:

<select>
<option value="1">Top1</option>
<option value="2">Top2</option>
<option value="9">Top3</option>
<option value="7"> - - Top3.1</option>
<option value="5"> - - Top3.2</option>
<option value="12">- - - - Top3.2.1</option>
<option value="6">Top4</option>
<option value="4">Top5</option>
<option value="8"> - - Top5.1</option>
<option value="3"> - - Top5.2</option>

I can’t work with optgroup, because everything is selectable. As far as I know, you can’t select optgroup labels.

My array looks like this:

[44] => Array
    (
        [id] => 1
        [name] => Test
        [slug] => test
        [parent] => 0
    )

[45] => Array
    (
        [id] => 2
        [name] => Test-Sub
        [slug] => test-sub
        [parent] => 1
    )

[46] => Array
    (
        [id] => 3
        [name] => Test-Sub-Sub
        [slug] => test-sub-sub
        [parent] => 2
    )

I am feeling like I have tried dozens of variantions, but I can’t build my form select right.

That was my last try:

function toDropdown($arr)
    {
        foreach ($arr as $row) {
            $cat[$row['id']] = $row['name'];
            if ($row['parent'] != 0) {
                $cat[$row['id']] = '--' . $row['name'];
            }
        }
        return $cat;
    }

But this way, it is ordered by the ID and the nesting loses its meaning.

I’ll try to go on, but if someone can help I appreciate any help!


EDIT: PHP Data


My function to get all categories from the DB:

function get_categories($parent = 'all')
{
    $this->db->select('categories.id, categories.name, categories.slug, categories.parent');
    $this->db->from('categories');

    if ($query = $this->db->get())
    {
        return $query->result_array();
    }

    return FALSE;
}

My view.php, where I output all data:

$query = $this->datei_model->get_categories('all');

foreach ($query as $row)
{
    $parents[] = $row;
}

$tree = buildTree($parents);

print("<select>\n");
printTree($tree);
print("</select>");
  • 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-18T05:52:53+00:00Added an answer on June 18, 2026 at 5:52 am

    Try this;

    function buildTree(Array $data, $parent = 0) {
        $tree = array();
        foreach ($data as $d) {
            if ($d['parent'] == $parent) {
                $children = buildTree($data, $d['id']);
                // set a trivial key
                if (!empty($children)) {
                    $d['_children'] = $children;
                }
                $tree[] = $d;
            }
        }
        return $tree;
    }
    
    
    $rows = array(
        array ('id' => 1, 'name' => 'Test 1', 'parent' => 0),
        array ('id' => 2, 'name' => 'Test 1.1', 'parent' => 1),
        array ('id' => 3, 'name' => 'Test 1.2', 'parent' => 1),
        array ('id' => 4, 'name' => 'Test 1.2.1', 'parent' => 3),
        array ('id' => 5, 'name' => 'Test 1.2.2', 'parent' => 3),
        array ('id' => 6, 'name' => 'Test 1.2.2.1', 'parent' => 5),
        array ('id' => 7, 'name' => 'Test 2', 'parent' => 0),
        array ('id' => 8, 'name' => 'Test 2.1', 'parent' => 7),
    );
    
    $tree = buildTree($rows);
    // print_r($tree);
    
    function printTree($tree, $r = 0, $p = null) {
        foreach ($tree as $i => $t) {
            $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) .' ';
            printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']);
            if ($t['parent'] == $p) {
                // reset $r
                $r = 0;
            }
            if (isset($t['_children'])) {
                printTree($t['_children'], ++$r, $t['parent']);
            }
        }
    }
    
    
    print("<select>\n");
    printTree($tree);
    print("</select>");
    

    Output;

    <select>
        <option value='1'>Test 1</option>
        <option value='2'>- Test 1.1</option>
        <option value='3'>- Test 1.2</option>
        <option value='4'>-- Test 1.2.1</option>
        <option value='5'>-- Test 1.2.2</option>
        <option value='6'>--- Test 1.2.2.1</option>
        <option value='7'>Test 2</option>
        <option value='8'>- Test 2.1</option>
    </select>
    

    And in your case;

    <select>
        <option value='1'>Baden-Württemberg</option>
        <option value='2'>- DMP-Verträge</option>
        <option value='50'>- Sprechstundenbedarf</option>
        <option value='52'>- Richtgrößen</option>
        <option value='53'>- Prüfungen</option>
        <option value='54'>- DMP-Verträge</option>
        <option value='55'>- Sonstige Verträge</option>
        <option value='3'>Berlin</option>
        <option value='62'>- DMP-Verträge</option>
        <option value='63'>- Prüfungen</option>
        <option value='64'>- Richtgrößen</option>
        <option value='65'>- Sonstige Verträge</option>
        <option value='66'>- Sprechstundenbedarf</option>
        <option value='4'>Brandenburg</option>
        <option value='67'>- DMP-Verträge</option>
        <option value='68'>- Prüfungen</option>
        <option value='69'>- Richtgrößen</option>
        <option value='70'>- Sonstige Verträge</option>
        <option value='71'>- Sprechstundenbedarf</option>
        <option value='5'>Bremen</option>
        <option value='72'>- DMP-Verträge</option>
        <option value='73'>- Prüfungen</option>
        <option value='74'>- Richtgrößen</option>
        <option value='75'>- Sonstige Verträge</option>
        <option value='76'>- Sprechstundenbedarf</option>
        <option value='7'>Hessen</option>
        <option value='6'>Hamburg</option>
        <option value='8'>Mecklenburg-Vorpommern</option>
        <option value='9'>Niedersachsen</option>
        <option value='10'>Nordrhein</option>
        <option value='11'>Rheinland-Pfalz</option>
        <option value='12'>Saarland</option>
        <option value='13'>Sachsen</option>
        <option value='14'>Sachsen-Anhalt</option>
        <option value='15'>Schleswig-Holstein</option>
        <option value='16'>Thüringen</option>
        <option value='17'>Westfalen-Lippe</option>
        <option value='51'>Richtgrössen</option>
        <option value='56'>Bayern</option>
        <option value='57'>- DMP-Verträge</option>
        <option value='58'>- Prüfungen</option>
        <option value='59'>- Richtgrößen</option>
        <option value='60'>- Sonstige Verträge</option>
        <option value='61'>- Sprechstundenbedarf</option>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Struggling with the basics here. I want to setup an array, and fill it
I'm struggling with trying to sort an array of dictionaries. My dictionaries have a
I'm really struggling to create a valid multidimensional JavaScript array with the following basic
I'm currently learning C and am struggling with how to iterate through an array
I am struggling to create an active record statement to do what I want,
I'm struggling with doing this, I want to basically do a database deleteAll where
I am struggling with some arrays with hashes inside. I want to parse them
I can successfully post an array to PHP using Ajax Post but I want
I am really struggling with linking menus together. The app I want to create
I´m struggling with the Pagination. I want to use the default functionality. The problem

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.