0

I have this array:

array
  0 => string '3,6' (length=3)
  3 => string '4,5' (length=3)
  4 => string '7,8' (length=3)
  8 => string '9' (length=1)

OR

array
  3 => 
    array
      4 => 
        array
          7 => null
          8 => 
            array
              9 => null
      5 => null
  6 => null

Every key is the id and value is the id for the childs of this parent.
ID of 0 means that (3 & 6) have no parent.
Now, i want to output an HTML list like:

  • 3
    • 4
      • 7
      • 8
        • 9
    • 5
  • 6
Stoic
  • 10,536
  • 6
  • 41
  • 60
  • 1
    Why are they strings instead of arrays? – Ignacio Vazquez-Abrams Dec 08 '10 at 21:02
  • well, we can convert them to array with an `explode`... I have this array in various forms, but I am unable to generate this list from it. e.g. I have added another such form in the question. – Stoic Dec 08 '10 at 21:04
  • possible duplicate of [How can I convert a series of parent-child relationships into a hierarchical tree?](http://stackoverflow.com/questions/2915748/how-can-i-convert-a-series-of-parent-child-relationships-into-a-hierarchical-tree) – Eric Dec 08 '10 at 21:05
  • 1
    @Eric It is similar, but not a duplicate. That question handles for child->parent relations, not parent->children relations. – Alin Purcaru Dec 08 '10 at 21:11
  • 1
    @Stoic: Go with the second form, if there's no additional overhead in doing so. – Eric Dec 08 '10 at 21:17
  • @Eric.. Yeah. Infact, as I commented to Alin's solution, I was achieving at first form after passing the second form in a function. Your solution helps me discard that function. :) – Stoic Dec 08 '10 at 21:21
  • 1
    Yea the second form is easily preferable. If that's what you had to begin with, I dont know why you'd go in the other direction. – jon_darkstar Dec 08 '10 at 21:25

2 Answers2

4
$arr = array(
  0 => '3,6',
  3 => '4,5',
  4 => '7,8',
  8 => '9',
);
function writeList($items){
    global $arr;
    echo '<ul>';

    $items = explode(',', $items);
    foreach($items as $item){
        echo '<li>'.$item;
        if(isset($arr[$item]))
            writeList($arr[$item]);
        echo '</li>';
    }

    echo '</ul>';
}
writeList($arr[0]);

Test it.

or

$arr = array(
    3 => array(
        4 => array(
            7 => null,
            8 => array(
                9 => null
            ),
        ),
        5 => null,
    ),
    6 => null,
);
function writeList($items){
    if($items === null)
        return;
    echo '<ul>';
    foreach($items as $item => $children){
        echo '<li>'.$item;
        writeList($children);
        echo '</li>';
    }
    echo '</ul>';
}
writeList($arr);
Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
  • 1
    +1. I was going to split this into tree-building and the html output, but i like how yours worked out here. very concise – jon_darkstar Dec 08 '10 at 21:10
  • Thank you, Alin.. Your function works awesomely well.. Though, just accepting Eric's answer because it saves me an intermediate function, I am using :) – Stoic Dec 08 '10 at 21:19
  • Well... I made the first function for the format you posted the first time. I added one for the second format as well, but being such a standard implementation its almost identical to Eric's. – Alin Purcaru Dec 08 '10 at 21:23
1

Taking this format:

$data = array(
    3 => array(
        4 => array(
            7 => null,
            8 => array(
                9 => null
            )
        ),
        5 => null
    ),
    6 => null
);

Do this:

function writeList($tree)
{
    if($tree === null) return;
    echo "<ul>";
    foreach($tree as $node=>$children)
        echo "<li>", $node, writeList($children), '</li>';
    echo "</ul>";
}

writeList($data);

Test it here: http://codepad.org/MNoW94YU

Eric
  • 95,302
  • 53
  • 242
  • 374
  • @Alin: oops. I spotted that just as I posted it! – Eric Dec 08 '10 at 21:15
  • Thank You.. That was the real function I was looking for.. Simple, sweet and really concise... :) Added some warning checks to suit it to my needs 100% :) – Stoic Dec 08 '10 at 21:16
  • What warning checks are needed, may I ask? – Eric Dec 08 '10 at 21:20
  • 1
    @Stoic: Wait, you actually __chose__ to turn your heirarchical array into a nightmarish hell of strings-representing-array-indeces? – Eric Dec 08 '10 at 21:21
  • @Eric.. Oh Yes. I definitely did. Because, I just had no idea on how to traverse a tree (recursion is one of the very basic things, we need to know and I just fail at it) – Stoic Dec 08 '10 at 21:23
  • @Eric.. Oh, at the time I copied the function you posted, it needed to be checked for `null` values (`foreach` loop) while recursing. I can see that you've added that check. :) – Stoic Dec 08 '10 at 21:31