Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to generate the HTML for tree like (jsTree) out of a 2d array with no success.

I have the following array: My Array

and from this array i would like to create a tree html (ul and li) structure like:

<ul id="ParentId-0">
    <li id="categoryID-1" data-parentid="1">
        bla bla
        <ul id="ParentId-1">
            <li id="categoryID-20" data-parentid="20">
                some Title
                <ul id="ParentId-20">......</ul>
            </li>
        </ul>
    </li>

    <li id="categoryID-2" data-parentid="2">
        second li Title
        <ul id="ParentId-2">
            <li id="categoryID-46" data-parentid="46">
                Another Title
                <ul id="ParentId-46">
                    <li id="categoryID-300" data-parentid="30">
                        And another Category
                        <ul id="ParentId-300"></ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

Anyone with an idea?

Edit

I've tried using DOMDocument to create the tree and it worked however it took like 40 - 50 seconds to load and i am trying to find a faster way.

share|improve this question
    
I'd recommend you start by looking for the root element (the one without a parent) and, recusivly, append to it all the elements that are its children. For exemple, you could have a function that takes an identifier as input, and that returns an array containing the elements that have this identifier as parent, recursivly. Once you have your tree as pure information, turn it into html (recursivly again) –  Virus721 Jun 14 '13 at 12:38
1  
use recursion :) –  Robert Jun 14 '13 at 12:42
    
Lol robert :-) yea ^^ –  Neta Meta Jun 14 '13 at 12:47
1  
HERE is a sample ;) –  mee Jun 14 '13 at 15:36
    
Ended up using DOMdocument works fast and correctly, but thanks alot for this link i will try it as well maybe its like a second faster –  Neta Meta Jun 14 '13 at 17:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.