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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:41:10+00:00 2026-06-18T10:41:10+00:00

I have an array with the following keys id parent_id name A sample array:

  • 0

I have an array with the following keys

id   
parent_id
name

A sample array:

array(7) {
  [0]=>
  array(3) {
    ["id"]=>
    string(1) "4"
    ["parent_id"]=>
    string(1) "0"
    ["name"]=>
    string(16) "Top Level Page 4"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(1) "5"
    ["parent_id"]=>
    string(1) "1"
    ["name"]=>
    string(19) "Second Level Page 1"
  }
  [2]=>
  array(3) {
    ["id"]=>
    string(1) "6"
    ["parent_id"]=>
    string(1) "2"
    ["name"]=>
    string(19) "Second Level Page 2"
  }
  [3]=>
  array(3) {
    ["id"]=>
    string(1) "7"
    ["parent_id"]=>
    string(1) "5"
    ["name"]=>
    string(18) "Third Level Page 1"
  }
  [4]=>
  array(3) {
    ["id"]=>
    string(1) "3"
    ["parent_id"]=>
    string(1) "0"
    ["name"]=>
    string(16) "Top Level Page 3"
  }
  [5]=>
  array(3) {
    ["id"]=>
    string(1) "2"
    ["parent_id"]=>
    string(1) "0"
    ["name"]=>
    string(16) "Top Level Page 2"
  }
  [6]=>
  array(3) {
    ["id"]=>
    string(1) "1"
    ["parent_id"]=>
    string(1) "0"
    ["name"]=>
    string(16) "Top Level Page 1"
  }
}

What I would like to do is display a hierarchy tree using this array, the code I have at the moment is producing:

Top Level Page 4
--Second Level Page 1
---Second Level Page 2
----Third Level Page 1
Top Level Page 3
Top Level Page 2
Top Level Page 1

Ideally I need to produce the below result but with unlimited levels:

Top Level Page 4
-Second Level Page 1
-Second Level Page 2
--Third Level Page 1
Top Level Page 3
Top Level Page 2
Top Level Page 1

The code I have so far is:

$level = 1;
        foreach ($data as $row) {
            if ($row['parent_id'] == 0) {
                echo $row['name'] . '<br/>';
            } else {
                $level++;
                foreach ($data as $m) {
                    if ($m['parent_id'] === $row['parent_id']) {
                        $c = 0;
                        $append = '';
                        while ($c < $level) {
                            $append.="-";
                            $c++;
                        }
                        echo $append . $row['name'] . '<br/>';
                    }
                }
            }
        }
    }

If anyone could give me some pointers on how to achieve this it would be much appreciated.

I found a solution here: Create nested list from PHP array for dropdown select field

  • 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-18T10:41:11+00:00Added an answer on June 18, 2026 at 10:41 am

    You should use recursion.

    Here an exemple of code:

    $datas = array(
        array('id' => 1, 'parent' => 0, 'name' => 'Page 1'),
        array('id' => 2, 'parent' => 1, 'name' => 'Page 1.1'),
        array('id' => 3, 'parent' => 2, 'name' => 'Page 1.1.1'),
        array('id' => 4, 'parent' => 3, 'name' => 'Page 1.1.1.1'),
        array('id' => 5, 'parent' => 3, 'name' => 'Page 1.1.1.2'),
        array('id' => 6, 'parent' => 1, 'name' => 'Page 1.2'),
        array('id' => 7, 'parent' => 6, 'name' => 'Page 1.2.1'),
        array('id' => 8, 'parent' => 0, 'name' => 'Page 2'),
        array('id' => 9, 'parent' => 0, 'name' => 'Page 3'),
        array('id' => 10, 'parent' => 9, 'name' => 'Page 3.1'),
        array('id' => 11, 'parent' => 9, 'name' => 'Page 3.2'),
        array('id' => 12, 'parent' => 11, 'name' => 'Page 3.2.1'),
        );
    
    function generatePageTree($datas, $parent = 0, $depth=0){
        $ni=count($datas);
        if($ni === 0 || $depth > 1000) return ''; // Make sure not to have an endless recursion
        $tree = '<ul>';
        for($i=0; $i < $ni; $i++){
            if($datas[$i]['parent'] == $parent){
                $tree .= '<li>';
                $tree .= $datas[$i]['name'];
                $tree .= generatePageTree($datas, $datas[$i]['id'], $depth+1);
                $tree .= '</li>';
            }
        }
        $tree .= '</ul>';
        return $tree;
    }
    
    echo(generatePageTree($datas));
    

    You can test it at: http://phpfiddle.org/main/code/1qy-5fj

    Or if you want the exact format:

    function generatePageTree($datas, $parent = 0, $depth = 0){
        $ni=count($datas);
        if($ni === 0 || $depth > 1000) return ''; // Make sure not to have an endless recursion
        $tree = '';
        for($i=0; $i < $ni; $i++){
            if($datas[$i]['parent'] == $parent){
                $tree .= str_repeat('-', $depth);
                $tree .= $datas[$i]['name'] . '<br/>';
                $tree .= generatePageTree($datas, $datas[$i]['id'], $depth+1);
            }
        }
        return $tree;
    }
    

    The test: http://phpfiddle.org/main/code/jw3-s1j

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following array, which I would like to reindex so the keys
I have an array structure like the following: I would like to have the
I have an array that looks like the following: array = [[1, 5], [4,
I have the following array of keys and values. How can i recreate this
I have the following issue, I have an array with the name $data Within
I have the following program that assigns dictionary keys to an array ( addr[]
Say that I have an array like the following: Array ( [arm] => Array
I have an array like the following: people = [{'node': 'john', 'dist': 3}, {'node':
I have the following line: [self.keys addObjectsFromArray:[newArray objectAtIndex:0]]; newArray is an array of arrays.
I have a form that looks like the following: <input type=text name=item[101][0][date] value=2012 />

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.