dsj60862 2013-12-05 15:11
浏览 28
已采纳

在UL中包装每个子密钥

I'm wanting to wrap each child array within an <ul>, but I'm not entirely sure how it may be done?

function RecursiveWrite($array) {
    foreach ($array as $v) {

        echo '<li>';

            echo $v['id'];

            if(isset($v['children']))
                RecursiveWrite($v['children']);

        echo '</li>';

    }
}

echo '<ul>';
RecursiveWrite($comments);
echo '</ul>';

Outputs:

    <ul>
        <li>81
            <li>173</li>
            <li>84
                <li>85
                    <li>131
                        <li>176</li>
                    </li>
                </li>
                <li>174</li>
                <li>175</li>
            </li>
        </li>
        <li>178</li>
    </ul>

The output I'm wanting to get:

<ul>
    <li>81
        <ul>
            <li>173</li>
            <li>84
                <ul>
                    <li>85
                        <ul>
                            <li>131
                                <ul>
                                    <li>176</li>
                                </ul>
                            </li>
                        </ul>
                    </li>
                    <li>174</li>
                    <li>175</li>
                </ul>
            </li>
        </ul>
    </li>
    <li>178</li>
</ul>

Original array:

array (
  0 => 
  array (
    'id' => '81',
    'parent' => NULL,
    'children' => 
    array (
      0 => 
      array (
        'id' => '173',
        'parent' => '81',
      ),
      1 => 
      array (
        'id' => '84',
        'parent' => '81',
        'children' => 
        array (
          0 => 
          array (
            'id' => '85',
            'parent' => '84',
            'children' => 
            array (
              0 => 
              array (
                'id' => '131',
                'parent' => '85',
                'children' => 
                array (
                  0 => 
                  array (
                    'id' => '176',
                    'parent' => '131',
                  ),
                ),
              ),
            ),
          ),
          1 => 
          array (
            'id' => '174',
            'parent' => '84',
          ),
          2 => 
          array (
            'id' => '175',
            'parent' => '84',
          ),
        ),
      ),
    ),
  ),
  1 => 
  array (
    'id' => '178',
    'parent' => NULL,
  ),
)
  • 写回答

1条回答 默认 最新

  • dongqie2355 2013-12-05 15:13
    关注

    It looks like you just need to echo out the <ul> tags before and after your foreach loop.

    Simple as...

    function RecursiveWrite($array) {
        echo "<ul>";
        foreach ($array as $v) {
            ...
        }
        echo "</ul>";
    }
    
    RecursiveWrite($comments);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP