0

我正在尝试对 Parent -child 和他们的孩子进行分类,等等我有一个数组,其中包含子项意味着子项。

$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
4

3 回答 3

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;
     }
}

   ?>
于 2013-04-11T13:15:43.753 回答
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;
}
于 2013-04-11T12:31:46.503 回答
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;
}

从 for each 循环中获取 ul 标签。

于 2013-04-11T12:53:26.127 回答