views:

215

answers:

2

hi.. i'm referring this address for function olLiTree

http://stackoverflow.com/questions/753853/php-function-that-creates-a-nested-ul-li

i have this array

$tree = array("A"=>array("B"=>array("C"=>"C","D"=>"D"),"E"=>array("F"=>"F","G"=>"G")));

but not able to use this function

function olLiTree($tree)
{
    echo '<ul>';
    foreach($tree as $item) {
        if (is_array($item)) {
            olLiTree($item);
        } else {
            echo '<li>', $item, '</li>';
        }
    }
    echo '</ul>';
}

to generate

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

can anybody help me to fix this? thanks..

+1  A: 

By using something like this it should work (I did not test it, so hopefully I did not make any embarrassing mistakes):

function olLiTree($tree)
{
    echo '<ul>';
    foreach($tree as $key => $item) {
        if (is_array($item)) {
            echo '<li>', $key;
            olLiTree($item);
            echo '</li>';
        } else {
            echo '<li>', $item, '</li>'; // Or use $key here as well
        }
    }
    echo '</ul>';
}

If you want to exactly match the given example you need to modify the array, since it does not match the result. (But I suppose that is not the problem)

Veger
this also not work. the array is: $tree = array("A"=>array("B"=>array("C"=>"C","D"=>"D"),"E"=>array("F"=>"F","G"=>"G")));
apis17
What is not working? I now tried and it showed the tree, which is different compared to your example tree, but that is due to the wrong $tree value.
Veger
any idea to convert that $tree value? maybe another function.
apis17
The example output you gave is incorrect HTML, the final `<ul>G</ul>` part is directly in the main `<ul></ul>` part, which is not allowed. Therefore the function is not able to perfectly reproduce it, but the `$tree` you give you also generate the same answer, except the `<ul>G</ul>` part which is put in the `<ul>E</ul>` part.But, it would really help if you tell what is wrong: You get an error? The tree you put into the function is not matching the output? You need other output?
Veger
veger: the HTML was generated for testing only. there are nothing wrong but just my array structure. the problem is solved. thank you all for suggestion :)
apis17
+2  A: 
function olLiTree($tree) {
    $out = '<ul>';

    foreach($tree as $key => $value) {
        $out.= '<li>';

        if (is_array($value)) {
            $out.= $key . olLiTree($value);
        } else {
            $out.= $value;
        }

        $out.= '</li>';
    }

    $out.= '</ul>';

    return $out;
}

Always return the final output, don't echo it directly. You'll thank yourself one day when you find a situation where you don't want to echo it immediately. :)

You might want to change the array structure to this, as it's less redundant (works with above code):

$tree = array('A', 'B', 'C' => array('CA', 'CB'), 'D');
deceze
thank you deceze.. didn't work as expected. or maybe actual array need to be adjusted to another format. do you think so?
apis17
@apis17 Updated answer.
deceze
hi.. any idea to change into that structure? thank you.
apis17
@apis17 ...come again? What's stopping you from writing your array like this?
deceze
deceze: the structure was generated by database query. have no idea to make it compatilbe to make output using ulLiTree function.
apis17
@apis17 Without seeing your query it's hard to answer, but the above code should work with either structure. As others have noted though, the array in your question and what you expect to get out of it doesn't match.
deceze
ok done! i've corrected sql query array structure. http://stackoverflow.com/questions/2423682/converting-arrays-into-another-format-php as appreciation i mark this as answer. thanks all.
apis17