1

我正在为网站构建导航。我的所有数据都存储在在线 JSON 上,这是一个示例:

{
    "State": 0, 
    "Tasks": [
        {
            "Id": 1000000, 
            "ParentId": 0, 
            "TaskCode": "1             ", 
            "Name": "item 1", 
            "Description": "item 1", 
            "Url": "", 
            "Parameter": ""
        }, 
        {
            "Id": 1010000, 
            "ParentId": 1000000, 
            "TaskCode": "1.1           ", 
            "Name": "item 1.1", 
            "Description": "item 1.1", 
            "Url": "", 
            "Parameter": ""
        }, 
        {
            "Id": 1010100, 
            "ParentId": 1010000, 
            "TaskCode": "1.1.1         ", 
            "Name": "item 1.1.1", 
            "Description": "item 1.1.1", 
            "Url": "", 
            "Parameter": ""
        }, 
        {
            "Id": 1010200, 
            "ParentId": 1010000, 
            "TaskCode": "1.1.2         ", 
            "Name": "item 1.1.2", 
            "Description": "item 1.1.2", 
            "Url": "", 
            "Parameter": ""
        }, 
        {
            "Id": 1010300, 
            "ParentId": 1010000, 
            "TaskCode": "1.1.3         ", 
            "Name": "item 1.1.3", 
            "Description": "item 1.1.3", 
            "Url": "", 
            "Parameter": ""
        }, 
        {
            "Id": 1010400, 
            "ParentId": 1010000, 
            "TaskCode": "1.1.4         ", 
            "Name": "item 1.1.4", 
            "Description": "item 1.1.4", 
            "Url": "", 
            "Parameter": ""
        }, 
        {
            "Id": 1020000, 
            "ParentId": 1000000, 
            "TaskCode": "1.2           ", 
            "Name": "item 1.2", 
            "Description": "item 1.2", 
            "Url": "", 
            "Parameter": ""
        }, 
        {
            "Id": 1030000, 
            "ParentId": 1000000, 
            "TaskCode": "1.3           ", 
            "Name": "item 1.3", 
            "Description": "item 1.3", 
            "Url": "", 
            "Parameter": ""
        }
    ]
}

然后我创建一个这样的函数:

function buildNavigation($items, $parent = '0')
{
    $hasChildren = false;
    $outputHtml = '<ul>%s</ul>';
    $childrenHtml = '';

    foreach($items as $item)
    {
        $curr_parent = $item->Id;
        if ($item->ParentId == $parent) {
            $hasChildren = true;
            $childrenHtml .= "<li>";
            $childrenHtml .= "<a href='" . $item->Url . "'><img alt='' src='xxx.png'>";
            $childrenHtml .= trim($item->TaskCode).". " . $item->Name . "</a>";         
            foreach($items as $child)
            {
                $cur_node = $child->Id;
                if ($child->ParentId == $curr_parent) {
                $hasChildren = true;
                $childrenHtml .= '<ul>';
                $childrenHtml .= '<li>';
                $childrenHtml .= '<a href="' . $child->Url . '">';
                $childrenHtml .= trim($child->TaskCode) . ". " . $child->Name . "</a>";
                    foreach($items as $child2)
                        {
                            $cur_node2 = $child2->Id;
                            if ($child2->ParentId == $cur_node) {
                                $hasChildren = true;
                                    $childrenHtml .= '<ul>';
                                    $childrenHtml .= '<li>';
                                    $childrenHtml .= '<a href="' . $child2->Url . '">';
                                    $childrenHtml .= trim($child2->TaskCode).". " . $child2->Name . "</a>";
                                    $childrenHtml .= '</li>';
                                    $childrenHtml .= '</ul>';
                            }
                        }
                $childrenHtml .= '</li>';
                $childrenHtml .= '</ul>';
                }
            }
            $childrenHtml .= '</li>';           
        }
    }

    // Without children, we do not need the <ul> tag.
    if (!$hasChildren) {
        $outputHtml = '';
    }

    // Returns the HTML
    return sprintf($outputHtml, $childrenHtml);
}

echo buildNavigation($json_data->Tasks);

但我在 html 上得到的是这样的:

<ul>
   <li>
      <a href=''><img alt='' src='xxx.png'>1. item 1</a>
      <ul>
         <li>
            <a href="">1.1. item 1.1</a>
            <ul>
               <li><a href="">1.1.1. item 1.1.1</a></li>
            </ul>
            <ul>
               <li><a href="">1.1.2. item1.1.2</a></li>
            </ul>
            <ul>
               <li><a href="">1.1.3. item1.1.3</a></li>
            </ul>
            <ul>
               <li><a href="">1.1.4. item1.1.4</a></li>
            </ul>

<!-- .... (and more) -->

应该是这样的:

<ul>
   <li>
      <a href=''><img alt='' src='xxx.png'>1. item1</a>
      <ul>
         <li>
            <a href="">1.1. item1.1</a>
            <ul>
               <li><a href="">1.1.1. item1.1.1</a></li>
               <li><a href="">1.1.2. item1.1.2</a></li>
               <li><a href="">1.1.3. item1.1.3</a></li>
               <li><a href="">1.1.4. item1.1.4</a></li>
            </ul>
         </li>
      </ul>
   </li>
</ul>

任何人都可以帮助改进我的 PHP 功能吗?

谢谢。

4

2 回答 2

3

这就是我认为你的代码应该是什么样子

$json = json_decode($json);
echo buildNavigation($json->Tasks);

输出

<ul>
    <li><a href="">1. Pelayanan Pelanggan</a>
    <ul>
            <li><a href="">1.1. Sambung Baru</a>
            <ul>
                    <li><a href="">1.1.1. Pendaftaran</a></li>
                    <li><a href="">1.1.2. Survey</a></li>
                    <li><a href="">1.1.3. BA Survey</a></li>
                    <li><a href="">1.1.4. SPK Penyambungan</a></li>
                </ul></li>
            <li><a href="">1.2. Koreksi Tarif</a></li>
            <li><a href="">1.3. Pemutusan Sambungan Atas Permintaan Pelanggan</a></li>
        </ul></li>
</ul>

使用的功能

function buildNavigation($items, $parent = '0') {
    $next = function ($items, $parent) {
        return array_filter($items, function ($v) use($parent) {
            return $v->ParentId == $parent;
        });
    };
    $output = "<ul>";
    foreach ( $next($items, $parent) as $item ) {
        $output .= '<li>';
        $output .= sprintf('<a href="%s">%s. %s</a>', $item->Url, trim($item->TaskCode), $item->Name);
        if ($next($items, $item->Id)) {
            $output .= buildNavigation($items, $item->Id);
        }
        $output .= '</li>';
    }
    $output .= "</ul>";
    return $output;
}
于 2013-04-06T12:51:50.270 回答
2

试试这个,希望它对你有用

function buildNavigation($items, $parent = '0')
{
    $hasChildren = false;
    $outputHtml = '<ul>%s</ul>';
    $childrenHtml = '';

    foreach($items as $item)
    {
        $curr_parent = $item->Id;
        if ($item->ParentId == $parent) {
            $hasChildren = true;
            $childrenHtml .= "<li>";
            $childrenHtml .= "<a href='" . $item->Url . "'><img alt='' src='xxx.png'>";
            $childrenHtml .= trim($item->TaskCode).". " . $item->Name . "</a>";         
            $childrenHtml .= '<ul>';
            foreach($items as $child)
            {
                $cur_node = $child->Id;
                if ($child->ParentId == $curr_parent) {
                $hasChildren = true;
                $childrenHtml .= '<li>';
                $childrenHtml .= '<a href="' . $child->Url . '">';
                $childrenHtml .= trim($child->TaskCode) . ". " . $child->Name . "</a>";
                    $childrenHtml .= '<ul>';
                    foreach($items as $child2)
                        {
                            $cur_node2 = $child2->Id;
                            if ($child2->ParentId == $cur_node) {
                                $hasChildren = true;

                                    $childrenHtml .= '<li>';
                                    $childrenHtml .= '<a href="' . $child2->Url . '">';
                                    $childrenHtml .= trim($child2->TaskCode).". " . $child2->Name . "</a>";
                                    $childrenHtml .= '</li>';

                            }
                        }
                $childrenHtml .= '</ul>';
                $childrenHtml .= '</li>';
                }
            }
            $childrenHtml .= '</ul>';
            $childrenHtml .= '</li>';           
        }
    }

    // Without children, we do not need the <ul> tag.
    if (!$hasChildren) {
        $outputHtml = '';
    }

    // Returns the HTML
    return sprintf($outputHtml, $childrenHtml);
}

echo buildNavigation($json_data->Tasks);
于 2013-04-06T12:37:24.907 回答