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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:52:34+00:00 2026-05-14T20:52:34+00:00

I’m using jsTree and I need to convert this HTML tag tree code <ul>

  • 0

I’m using jsTree and I need to convert this HTML tag tree code <ul> <li> to a PHP array.
The jsTree HTML tag will be passed to PHP to be parsed and store in a structured tree PHP array(see below for the PHP array structure).

Additional question: Is my desired PHP array structure good or you can suggest a good structure? I’m open for suggestions.

Thanks in advance 🙂

Cheers,
Mark

jsTree Screenshot:

alt text

HTML Tree String:

<ul class="ltr">
<li id="phtml_1" class="  open">
    <a style="" class=" " href="#"><ins>&nbsp;</ins>Folder 1</a>
    <ul>
    <li class="leaf" id="phtml_2">
        <a style="" class=" " href="#"><ins>&nbsp;</ins>Child 1.1</a>
    </li>
    <li class="open" id="phtml_3">
        <a style="" class=" " href="#"><ins>&nbsp;</ins>Folder 1.1</a>
        <ul>
        <li class="leaf last" rel="default">
            <a href="" style="" class=" "><ins>&nbsp;</ins>Child 1.1.1</a>
        </li>
        </ul>
    </li>
    <li class="last open" rel="default">
        <a href="" style="" class=" "><ins>&nbsp;</ins>Folder 1.2</a>
        <ul>
        <li class="leaf" rel="default">
            <a href="" style="" class=" "><ins>&nbsp;</ins>Child 1.2.1</a>
        </li>
        <li class="leaf last" rel="default">
            <a href="" style="" class=" "><ins>&nbsp;</ins>Child 1.2.2</a>
        </li>
        </ul>
    </li>
    </ul>
</li>
<li id="phtml_5" class="file open">
    <a style="" class=" " href="#"><ins>&nbsp;</ins>Folder 2</a>
    <ul>
    <li class="leaf" rel="default">
        <a href="" style="" class=" "><ins>&nbsp;</ins>Child 2.1</a>
    </li>
    <li class="leaf last" rel="default">
        <a href="" style="" class="clicked"><ins>&nbsp;</ins>Child 2.2</a>
    </li>
    </ul>
</li>
<li class="leaf last" rel="default">
    <a href="" style="" class=" "><ins>&nbsp;</ins>Outer Child</a>
</li>
</ul>

PHP Array Structure:

<?php
$tree_array = array(
    'Folder 1' => array(
        'Child 1.1',
        'Folder 1.1' => array(
            'Child 1.1.1'
        ),
        'Folder 1.2' => array(
            'Child 1.2.1',
            'Child 1.2.2'
        ),
    ),
    'Folder 2' => array(
        'Child 2.1',
        'Child 2.2'
    ),
    'Outer Child'
);

echo '<pre>',print_r($tree_array),'</pre>';
?>

PHP print_r Output:

Array
(
    [Folder 1] => Array
        (
            [0] => Child 1.1
            [Folder 1.1] => Array
                (
                    [0] => Child 1.1.1
                )

            [Folder 1.2] => Array
                (
                    [0] => Child 1.2.1
                    [1] => Child 1.2.2
                )

        )

    [Folder 2] => Array
        (
            [0] => Child 2.1
            [1] => Child 2.2
        )

    [0] => Outer Child
)
  • 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-14T20:52:34+00:00Added an answer on May 14, 2026 at 8:52 pm

    Regarding:

    My problem I’m having a hard time parsing the string HTML tag and save it in a PHP array.

    I suggest that you use an HTML parser, such as simplehtmldom. This will allow you to traverse over HTML DOM just the way you want.

    Here is a quick and dirty UL walking script:

    <?php
        require_once( "simplehtmldom/simple_html_dom.php" );
        $DOM = file_get_html( "test.htm" );
        $ARR = array( );
        function WalkUL( $ul, &$ar )
        {
            foreach( $ul->children as $li )
            {
                if ( $li->tag != "li" )
                {
                    continue;
                }
                $arar = array( );
                foreach( $li->children as $ulul )
                {
                    if ( $ulul->tag != "ul" )
                    {
                        continue;
                    }
                    WalkUL( $ulul, $arar );
                }
                $ar[ $li->find( "a", 0 )->plaintext ] = $arar;
            }
        }
        WalkUL( $DOM->find( "ul", 0 ), $ARR );
        print_r( $ARR );
    ?>
    

    Its output, not exactly as you wanted it but close:

    Array
    (
        [Folder 1] => Array
            (
                [Child 1.1] => Array
                    (
                    )
                [Folder 1.1] => Array
                    (
                        [Child 1.1.1] => Array
                            (
                            )
                    )
                [Folder 1.2] => Array
                    (
                        [Child 1.2.1] => Array
                            (
                            )
                        [Child 1.2.2] => Array
                            (
                            )
                    )
            )
        [Folder 2] => Array
            (
                [Child 2.1] => Array
                    (
                    )
                [Child 2.2] => Array
                    (
                    )
            )
        [Outer Child] => Array
            (
            )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 385k
  • Answers 385k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using Zip files with Adobe Air, Flex and Flash |… May 14, 2026 at 11:37 pm
  • Editorial Team
    Editorial Team added an answer The error has nothing to do with your inner class.… May 14, 2026 at 11:37 pm
  • Editorial Team
    Editorial Team added an answer Add a pre-build action to delete: %USERPROFILE%\Local Settings\Application Data\[AppName]\...\[Version]\user.config or… May 14, 2026 at 11:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.