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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:37:39+00:00 2026-05-29T03:37:39+00:00

I can’t for the life of me convert my array to a nested UL

  • 0

I can’t for the life of me convert my array to a nested UL LI tree, I’ve been attempting it all day but there must be some flaw that I cannot spot (probably because my array isn’t a very simple one).

Basically the array takes the form:

$array = array(
  "0" => array(
    "0" => array("letter" => "A", "data" => "123"), 
    "1" => array(
      "0" => array(
        "0" => array("letter" => "B", "data" => "123"), 
        "1" => array(
          "0" => array(
            "0" => array("letter" => "C", "data" => "123")
          )
        )
      ), 
      "1" => array(
        "0" => array("letter" => "D", "data" => "123"), 
        "1" => array(
          "0" => array(
            "0" => array("letter" => "E", "data" => "123")
          )
        )
      )
    )
  )
);

Explaining Array Above

The arrays come in pairs – the first number being the group, and the second being the row. The above has 3 columns (or levels as I call them) and there is only has one ‘group’.

So [0][0] would be group 0, first row, [0][1][0][0] would be the second row of level one, but is the first row of level 2, and so one.

Where basically I want the result to appear as follows (for the example above):

<ul id="mylist">
    <li>
        <ul class="initial">
            <li>A</li>
            <li class="top">
                <ul class="wrap">
                    <li>
                        <ul class="inner">
                            <li>B</li>
                            <li class="top">
                                <ul class="wrap">
                                    <li>
                                        <ul class="inner">
                                            <li>C</li>
                                        </ul>
                                    </li>
                                    <li class="end"><a class="add">Add</a></li>
                                </ul>
                            </li>
                        </ul>
                    </li>
                    <li>
                        <ul class="inner">
                            <li>D</li>
                            <li class="top">
                                <ul class="wrap">
                                    <li>
                                        <ul class="inner">
                                            <li>E</li>
                                        </ul>
                                    </li>
                                    <li class="end"><a class="add">Add</a></li>
                                </ul>
                            </li>
                        </ul>
                    </li>
                    <li class="end"><a class="add">Add</a></li>
                </ul>
            </li>
        </ul>
    </li>
    <li class="end"><a class="add">Add</a></li>
</ul>

Basically, an ‘add’ appears at the end of each node branch (last list item within ‘wrap’ class).

I’ve done the following so far, but I feel it isn’t correct at all, and doesn’t work correctly if additional nodes were added or nodes were removed.

function recursion($data, $level){

    $skip = array_key_exists('data', $data[0]) ? 0 : 1;

    if($skip == 0){
        $level++;
        if($level !== 1){
            $out .= "<li>\n";
        }
        if($level == 1){
            $out .= "<ul class='initial'>\n";
        } else {
            $out .= "<ul class='inner'>\n";
        }
    }
    $count = 0;
    $loop_count = 0;
    $show_end = 0;
    foreach($data as $key => $value){
        $loop_count++;
        if(is_array($value) && array_key_exists('data', $value)){
            $count++;

            $out .= ($skip == 0) ? "<li>" : "";

            if($value['data'] == "123"){
                $out .= $value['letter'];
            }

            if(!is_array($data[($key+1)][0])){
                $out .= "</li>\n";
                $out .= "<li class=\"end\"><a class=\"add\">Add</a></li>\n";

                $out .= "</ul>\n";
                $out .= "</li>\n";
                $out .= "</ul>\n";
                $out .= "</li>\n";

                $out .= "<li class=\"end\"><a class=\"add\">Add</a></li>\n";

                $show_end = 1;
            }
            $last_key = $key;
        } else if(is_array($value)){
            $out.= recursion($value, $level);
        }
        if($show_end == 1){
            $out .= "</ul>\n";
        }
        if(is_array($value) && array_key_exists('data', $value) && !is_array($data[($key+1)])){
            $out .= "</li>\n";
        }
        if($show_end == 1 && count($data) !== $count){
            $out .= "<li>\n";
        }
    }

    return $out;
}

Output of

 print_r($array, true) 

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [letter] => A
                    [data] => 123
                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [letter] => B
                                            [data] => 123
                                        )

                                    [1] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [letter] => C
                                                            [data] => 123
                                                        )

                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [0] => Array
                                        (
                                            [letter] => D
                                            [data] => 123
                                        )

                                    [1] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [letter] => E
                                                            [data] => 123
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)
  • 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-05-29T03:37:40+00:00Added an answer on May 29, 2026 at 3:37 am

    Are you sure your array structure matches the HTML you have shown? I tweaked the array and came up with this function that seems to work.

    function build_list($array, $level=0)
    {
        foreach($array as $group)
        {
            echo '<li>';
            if(isset($group['letter']))
            {
                echo $group['letter'];
            }
            else            
            {
                $class = 'wrap';
                if($level%2 == 1)
                {
                    $class = 'inner';
                }
                echo '<ul class="' . $class . '">';
                build_list($group, $level+1);
                if($class == 'wrap')
                {
                    echo '<li class="end"><a class="add">Add</a></li>';
                }
                echo '</ul>';
            }
            echo '</li>';
        }
    }
    

    Here are my changes to the array:

    $array = 
    array
    (
        array("letter" => "A", "data" => "123"), //li
        array //li ul
        (
            array //li ul
            (
                array("letter" => "B", "data" => "123"), //li
                array //li ul
                (
                    array //li ul
                    (
                        array("letter" => "C", "data" => "123")
                    )
                )
            ),
            array
            (
                array("letter" => "D", "data" => "123"), 
                array
                (
                    array
                    (
                        array("letter" => "E", "data" => "123")
                    )
                )
            )
        )
    );
    

    Then to call it:

    <ul class="inital">
    <?php
    build_list($array);
    ?>
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Can anybody tell me a regular expression to use within some PHP to find
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
Can anyone give me some tips on adding a custom button for each row
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Can someone tell me where I'm going wrong here? basically I've written some code
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Can someone please tell me is there a way to solve difference equation e.g:
can't seem to find what the problem is. firefox works fine - but safari

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.