loop out the result in php
<p>i want to loop out the $tree array, if it has child add class='parent' to the li,if don't have then use the default style(li). add the  and ul label out of all the child li.</p>

<p><code>print_r($tree)</code> result is</p>

<pre><code>Array
(
[0] =&gt; stdClass Object
(
[tid] =&gt; 6
[vid] =&gt; 2
[name] =&gt; parent one
[depth] =&gt; 0
[parents] =&gt; Array
(
[0] =&gt; 0
)

)

[1] =&gt; stdClass Object
(
[tid] =&gt; 14
[vid] =&gt; 2
[name] =&gt; child one
[depth] =&gt; 1
[parents] =&gt; Array
(
[0] =&gt; 6
)

)

[2] =&gt; stdClass Object
(
[tid] =&gt; 13
[vid] =&gt; 2
[name] =&gt; child two
[depth] =&gt; 1
[parents] =&gt; Array
(
[0] =&gt; 6
)

[6] =&gt; stdClass Object
(
[tid] =&gt; 8
[vid] =&gt; 2
[name] =&gt;parent three
[depth] =&gt; 0
[parents] =&gt; Array
(
[0] =&gt; 0
)
</code></pre>

<p>I want to output the result as this:</p>

<pre><code>&lt;li class='parent'&gt;
&lt;a href="#"&gt;parent one&lt;/a&gt;&lt;span&gt;&lt;/span&gt;
&lt;ul class='haschild'&gt;
&lt;li&gt;&lt;a href="#"&gt;child one/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;child two&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=""&gt;child three&lt;/a&gt;&lt;/li&gt;
</code></pre>

<p>I used the following code, but it can't output the above html which I want to </p>

<pre><code>foreach($tree as $term){
    if($term-&gt;depth==0){
        echo "&lt;li class='parent'&gt;&lt;a href=''&gt;$term-&gt;name&lt;/a&gt;&lt;span&gt;&lt;/span&gt;";
    }
    if($term-&gt;depth&gt;0){
        echo "&lt;ul&gt;&lt;li&gt;&lt;a href=''&gt;$term-&gt;name&lt;/a&gt;&lt;li&gt;&lt;/ul&gt;";
    }
    echo "&lt;/li&gt;";
}
</code></pre>

<p>The output of the above is terrible. Where am I going wrong and how do I fix this?</p>
