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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:07:36+00:00 2026-06-12T10:07:36+00:00

I have an array in PHP, which looks like this: array ( [0] =>

  • 0

I have an array in PHP, which looks like this:

array (
    [0] => array (
        [id] => 1
        [title] => "Title 1"
        [parent_id] => NULL
        [depth] => 0
    )
    [1] => array (
        [id] => 2
        [title] => "Title 2"
        [parent_id] => NULL
        [depth] => 0
    )
    [2] => array (
        [id] => 3
        [title] => "Title 3"
        [parent_id] => 2
        [depth] => 1
    )
    [3] => array (
        [id] => 4
        [title] => "Title 4"
        [parent_id] => 2
        [depth] => 1
    )
    [4] => array (
        [id] => 5
        [title] => "Title 5"
        [parent_id] => NULL
        [depth] => 0
    )
    [5] => array (
        [id] => 6
        [title] => "Title 6"
        [parent_id] => 4
        [depth] => 2
    )
)

What i want to do is iterate over this array and create a nested <ol> list from it. So the result should look like this:

<ol>
    <li>Title 1</li> // id = 1
    <li>Title 2</li> // id = 2
    <ol>
        <li>Title 3</li> // id = 3 -> parent_id = 2
        <li>Title 4</li> // id = 4 -> parent_id = 2
        <ol>
            <li>Title 6</li> // id = 6 -> parent_id = 4
        </ol>
    </ol>
    <li>Title 5</li> // id = 5
</ol>

I’ve been trying to think of a way how i could get this done. But so far every attempt failed…

Anyone any idea how i can create such a nested <ol> list from an array like that?

Please note that i do not have any control on the given data. I simply make a call to an API and it returns json data, which i convert to an array. And the array looks exactly like the one i described.

  • 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-12T10:07:38+00:00Added an answer on June 12, 2026 at 10:07 am

    You should use recursion:

    First the array in ‘php’ syntax:

    <?php
    $a=array (
        '0' => array (
            'id' => 1,
            'title' => "Title 1",
            'parent_id' => 'NULL',
            'depth' => 0
        ),
        '1' => array (
            'id' => 2,
            'title' => "Title 2",
            'parent_id' => 'NULL',
            'depth' => 0
        ),
        '2' => array (
            'id' => 3,
            'title' => "Title 3",
            'parent_id' => 2,
            'depth' => 1
        ),
        '3' => array (
            'id' => 4,
            'title' => "Title 4",
            'parent_id' => 2,
            'depth' => 1
        ),
        '4' => array (
            'id' => 5,
            'title' => "Title 5",
            'parent_id' => 'NULL',
            'depth' => 0
        ),
        '5' => array (
            'id' => 6,
            'title' => "Title 6",
            'parent_id' => 4,
            'depth' => 0
        )
    );
    

    Here the code:

    $level = 'NULL';
    
    function r( $a, $level) {
       $r = "<ol>";
       foreach ( $a as $i ) {
           if ($i['parent_id'] == $level ) {
              $r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
           }
       }
       $r = $r . "</ol>";
       return $r;
    }
    
    print r( $a, $level );
    
    ?>
    

    The results:

    <ol><li>Title 1<ol></ol></li><li>Title 2<ol><li>Title 3<ol>
    </ol></li><li>Title 4<ol><li>Title 6<ol></ol></li></ol></li></ol></li><li>Title 5
    <ol></ol></li></ol>
    
    1. Title 1\n
      1. Title 2\n
        1. Title 3\n
          1. Title 4\n
            1. Title 6\n
          2. Title 5\n

            EDITED AFTER CHECK AS SOLUTION

            To avoid empty leafs:

            function r( $a, $level) {
               $r = '' ;
               foreach ( $a as $i ) {
                   if ($i['parent_id'] == $level ) {
                      $r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
                   }
               }
               return ($r==''?'':"<ol>". $r . "</ol>");
            }
            
            • 0
            • Reply
            • Share
              Share
              • Share on Facebook
              • Share on Twitter
              • Share on LinkedIn
              • Share on WhatsApp
              • Report

          Sidebar

          Related Questions

          I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
          I have a php multidimensional array which looks like this: $fields = array( array('input',
          I have an array in PHP which looks like this: Array ( [2] =>
          I have a PHP array which looks like this: Array ( [0] => Array
          I have an array being passed from php which looks like: $resultsArr[123]['A']='q'; $resultsArr[123]['B']='d'; $resultsArr[113]['C']='s';
          i have an array which looks like this: Array ( [0] => Array (
          I have an array $attribGarden which looks like this: array(3) { [0]=> array(2) {
          i have an array in php like this: $arr['test'] = asd; $arr['test1'] = asd1;
          I have an array that I pull that looks like this: [157966745,275000353,43192565,305328212] ... How
          I have to have a result which looks like this: ({1: 'string', 2: 'anotherstring',

          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.