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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:08:45+00:00 2026-05-28T18:08:45+00:00

This is a follow-up to my question yesterday – Recursive UL LI to PHP

  • 0

This is a follow-up to my question yesterday – Recursive UL LI to PHP multi-dimensional array – I’ve almost managed to convert the HTML block to an array, though there is a slight problem that I cannot fix. When processing the HTML block below, the output array does not quite follow what has been inputted (and I cannot see where I’m going wrong and need a fresh pair of eyes!!).

I’ve included the following items:

  • HTML Block
  • PHP Function and Processing
  • Output

HTML Block

Basically takes the form of:

-A
  -B
    -C
----
-D
  -E
    -F
----
-G
  -H
    -I

As follows:

<li>
    <ul>
        <li>A</li>
        <li>
            <ul>
                <li>B</li>
                <li>
                    <ul>
                        <li>C</li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</li>
<li>
    <ul>
        <li>D</li>
        <li>
            <ul>
                <li>E</li>
                <li>
                    <ul>
                        <li>F</li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</li>
<li>
    <ul>
        <li>G</li>
        <li>
            <ul>
                <li>H</li>
                <li>
                    <ul>
                        <li>I</li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</li>

PHP Function and Processing

function process_ul($output_data, $data, $key, $level_data, $level_key){

    if(substr($data[$key], 0, 3) == '<ul'){
        // going down a level in the tree
        $level_key++;

        // check to see if the level key exists within the level data, else create it and set to zero
        if(!is_numeric($level_data[$level_key])){
            $level_data[$level_key] = 0;
        }

        // increment the key to look at the next line
        $key++;

        if(substr($data[$key], 0, 4) !== '</ul'){
            while(substr($data[$key], 0, 4) !== '</ul'){
                // whilst we don't have an end of list, do some recursion and keep processing the array

                $returnables = process_ul($output_data, $data, $key, $level_data, $level_key);
                $output_data = $returnables['output'];
                $data = $returnables['data'];
                $key = $returnables['key'];
                $level_data = $returnables['level_data'];
                $level_key = $returnables['level_key'];
            }
        }
    }

    if(substr($data[$key], 0, 4) !== '</ul' && $data[$key] !== "<li>" && $data[$key] !== "</li>"){
        // we don't want to be saving lines with no data or the ends of a list

        // get the array key value so we know where to save it in our array (basically so we can't overwrite anything that may already exist
        $this_key = &$output_data;
        for($build_key=0;$build_key<($level_key+1); $build_key++){
            $this_key =& $this_key[$level_data[$build_key]];
        }

        if(is_array($this_key)){
            // look at the next key, find the next open one
            $this_key[(array_pop(array_keys($this_key))+1)] = $data[$key];
        } else {
            // a new entry, so nothing to worry about
            $this_key = $data[$key];
        }
        $level_data[$level_key]++;
    } else if(substr($data[$key], 0, 4) == '</ul'){
        // going up a level in the tree
        $level_key--;
    }

    // increment the key to look at the next line when we loop in a moment
    $key++;

    // prepare the data to be returned
    $return_me = array();
    $return_me['output'] = $output_data;
    $return_me['data'] = $data;
    $return_me['key'] = $key;
    $return_me['level_data'] = $level_data;
    $return_me['level_key'] = $level_key;

    // return the data
    return $return_me;
}


// explode the data coming in by looking at the new lines
$input_array = explode("\n", $html_ul_tree_in); 

// get rid of any empty lines - we don't like those
foreach($input_array as $key => $value){
    if(trim($value) !== ""){
        $input_data[] = trim($value);
    }
}

// set the array and the starting level
$levels = array();
$levels[0] = 0;
$this_level = 0;

// loop around the data and process it
for($i=0; $i<count($input_data); $i){
    $returnables = process_ul($output_data, $input_data, $i, $levels, $this_level);
    $output_data = $returnables['output'];
    $input_data = $returnables['data'];
    $i = $returnables['key'];
    $levels = $returnables['level_data'];
    $this_level = $returnables['level_key'];
}

// let's see how we did
print_r($output_data);

Output

Note that D is in the wrong position, should be in position [0][2] – not [0][1][2], and every other position after D is out by 1 place (I’m sure you can tell by looking).

Basically takes the form of:

-A
  -B
    -C
  -D
----
  -E
    -F
  -G
----
  -H
    -I

As follows:

Array
(
    [0] => Array
        (
            [0] => <li>A</li>
            [1] => Array
                (
                    [0] => <li>B</li>
                    [1] => Array
                        (
                            [0] => <li>C</li>
                        )

                    [2] => <li>D</li>
                )

            [2] => Array
                (
                    [1] => <li>E</li>

                    [2] => Array
                        (
                            [1] => <li>F</li>
                        )

                    [3] => <li>G</li>
                )

            [3] => Array
                (
                    [2] => <li>H</li>
                    [3] => Array
                        (
                            [2] => <li>I</li>
                        )

                )

        )

)

Thanks for your time – any assistance in outputting the array correctly would be greatly appreciated!

  • 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-28T18:08:46+00:00Added an answer on May 28, 2026 at 6:08 pm

    IF your lists are always well formed, you could use this to do what you want. It uses SimpleXML so it will not be forgiving of mistakes and bad form in the input code. If you want to be forgiving, you will need to use DOM – the code will be a little more complex, but not ridiculously so.

    function ul_to_array ($ul) {
      if (is_string($ul)) {
        if (!$ul = simplexml_load_string("<ul>$ul</ul>")) {
          trigger_error("Syntax error in UL/LI structure");
          return FALSE;
        }
        return ul_to_array($ul);
      } else if (is_object($ul)) {
        $output = array();
        foreach ($ul->li as $li) {
          $output[] = (isset($li->ul)) ? ul_to_array($li->ul) : (string) $li;
        }
        return $output;
      } else return FALSE;
    }
    

    It takes data in the exact form as provided in the question – with no outer enclosing <ul> tags. If you want to pass the outer <ul> tags as part of the input string, just change

    if (!$ul = simplexml_load_string("<ul>$ul</ul>")) {
    

    to

    if (!$ul = simplexml_load_string($ul)) {
    

    See it working

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

Sidebar

Related Questions

This is a follow-up to my question from yesterday . I have Scott Meyers'
This is a follow up question from Problem with array assignment I now have
This is a follow-up to a question I asked yesterday. What I want to
this is actually a follow up to a question I asked yesterday about problems
This is a follow-up to my question yesterday . The function adds to a
this is a follow-up to the question I posted yesterday. I was able to
As a follow up question to this one I asked yesterday , are there
This is a follow up question to this one: Syntax confusion (do block) So,
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
This is a follow on question to How do I delete 1 file from

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.