PHP中的父母子女及其子女关系

时间:2013-04-11 12:22:50

标签: php

我正在努力创建父母和他们的孩子,等等 我有一个数组,其子项是指子项目。

$navarray= Array
(
    [0] => Array
        (
            [id] => 3
            [name] => add
            [controller] => ClassYearsSubjectsController
            [action] => getsubjects
            [parent] => 0
            [sub] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 1
            [name] => occupations
            [controller] => OccupationsController
            [action] => index
            [parent] => 0
            [sub] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [name] => test
                            [controller] => AddressesController
                            [action] => index
                            [parent] => 1
                            [sub] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 8
                                            [name] => testnew
                                            [controller] => AdmissionEnquiriesController
                                            [action] => giveadmissions
                                            [parent] => 7
                                            [sub] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 2
                            [name] => add
                            [controller] => OccupationsController
                            [action] => add
                            [parent] => 1
                            [sub] => Array
                                (
                                )

                        )

                )

        )

)

我正在尝试生成树。 但我试过

<?php echo GenerateNavHTML($navarray);  ?>

和功能 GenerateNavHTML

<?php 

function GenerateNavHTML($nav)
{ 

    $html = '';
    foreach($nav as $page)
    {
        $html.='<ul><li>';    

        $html .= '<a href="#">k</a>';
        $html .= GenerateNavHTML($page['sub']);
        $html .= '</li></ul>';
    }
    return $html;
}

   ?>

我的输出是

<ul style="display: block;" class="nav">
    <li class="test"><a href="">add</a>
    </li>
</ul><ul style="display: block;" class="nav"><li class="test"><a href="">occupations</a><ul style="display: block;" class="nav"><li class="test"><a href="">test</a><ul style="display: block;" class="nav"><li class="test"><a href="">testnew</a></li></ul></li></ul><ul style="display: block;" class="nav"><li class="test"><a href="">add</a></li></ul></li></ul>

但我期待这个输出。

<ul style="display: block;" class="nav">
    <li class="test">
    <li class="test">
        <a class="parent" href="#">Shoes</a>
        <ul>
            <li>
                <a class="parent" href="#">Womens</a>
                <ul>
                    <li><a href="#">Sandals</a></li>
                    <li><a href="#">Sneakers</a></li>
                    <li><a href="#">Wedges</a></li>
                    <li><a href="#">Heels</a></li>
                    <li><a href="#">Loafers</a></li>
                    <li><a href="#" class="last" >Flats</a></li>
                </ul>
            </li>
            <li>
                <a class="parent last"  href="#">Mens</a>
                <ul>
                    <li><a href="#">Loafers</a></li>
                    <li><a href="#">Sneakers</a></li>
                    <li><a href="#">Formal</a></li>
                </ul>
            </li>
        </ul>
    </li>
    </ul>
Can anybody tell what logic i need to change
Thanks

3 个答案:

答案 0 :(得分:1)

试试这个

<?php 

function GenerateNavHTML($nav)
{ 
    if(!empty($nav)) {

          if(isset($nav[0]['parent'])) {
                 $parentid=$nav[0]['parent'];
          }else{
              $parentid=1;
          }
         if($parentid==0) {
             $html = '<ul style="display: block;" class="nav">';
         }else {
             $html='<ul>';
         }
           foreach($nav as $page) {
                $html.='<li>';    
                $html .= '<a href="#">k</a>';
                $html .= GenerateNavHTML($page['sub']);
                $html .= '</li>';
            }
            $html.='</ul>';
            return $html;
     }
}

   ?>

答案 1 :(得分:0)

尝试这样做:

<?php 

function GenerateNavHTML($nav)
{ 
    $html = '<ul>';
    foreach ($nav as $page) {
        $html.='<li>';
        $html .= '<a href="#">k</a>';
        $html .= GenerateNavHTML($page['sub']);
        $html .= '</li>';
    }

    $html .= '</ul>';
    return $html;
}

答案 2 :(得分:0)

你能试试这个方法吗?

function GenerateNavHTML($nav)
{
    if (empty ($nav))
        return;

    $html = '';
    $html.='<ul>';

        foreach($nav as $page)
        {
            $html.='<li>';
                $html .= '<a href="#">'.$page["name"].'</a>';
                $html .= GenerateNavHTML($page['sub']);
            $html .= '</li>';
        }

    $html.='</ul>';

    return $html;
}

从每个循环中获取ul标记。