PHP将多维数组转换为嵌套列表

时间:2016-05-30 14:00:13

标签: php arrays list multidimensional-array nested

我从MYSQL查询得到结果,我正在努力将这个数组转换为嵌套列表:

Array(

[0] => Array
    (
        [post_title] => Esprit
        [post_id] => 240
        [folder_id] => 236
    )

[1] => Array
    (
        [post_title] => GC
        [post_id] => 241
        [folder_id] => 236
    )

[2] => Array
    (
        [post_title] => Guess
        [post_id] => 242
        [folder_id] => 236
        [children] => Array
            (
                [0] => Array
                    (
                        [post_title] => Jewels
                        [post_id] => 250
                        [folder_id] => 242
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [post_title] => Products-shots
                                        [post_id] => 251
                                        [folder_id] => 250
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [post_title] => New-Collection
                                                        [post_id] => 252
                                                        [folder_id] => 251
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

[3] => Array
    (
        [post_title] => Guess-Connect
        [post_id] => 243
        [folder_id] => 236
    )

[4] => Array
    (
        [post_title] => Nautica
        [post_id] => 244
        [folder_id] => 236
    )

[5] => Array
    (
        [post_title] => Obaku
        [post_id] => 245
        [folder_id] => 236
    )

[6] => Array
    (
        [post_title] => Police
        [post_id] => 246
        [folder_id] => 236
    )

[7] => Array
    (
        [post_title] => Roamer
        [post_id] => 247
        [folder_id] => 236
    )

[8] => Array
    (
        [post_title] => Superdry
        [post_id] => 248
        [folder_id] => 236
    )

[9] => Array
    (
        [post_title] => Vulcain
        [post_id] => 249
        [folder_id] => 236
    )

)

我尝试了很多代码,但没有人可以得到这个精确的输出:

<ul>
    <li>Esprit</li>
    <li>GC</li>
    <li>Guess
        <ul>
            <li>Jewels</li>
            <li>Products-shot</li>
            <li>New-collection</li>
        </ul>
    </li>
    <li>Guess-Connect</li>
    <li>Nautica</li>
    <li>Obaku</li>
    <li>Police</li>
    <li>Roamer</li>
    <li>Superdry</li>
    <li>Vulcain</li>
</ul>

我真的很感激帮助!提前致谢

3 个答案:

答案 0 :(得分:1)

你只能放二级显示试试这个:

function second_level_list( $arr ){
  foreach( $arr as $item ){
     echo '<li>';
     echo $item['post_title'];
     echo '</li>';
     if( isset($item['children']) && is_array($item['children']) ){
       second_level_list( $item['children']);
     }
  }
}

<ul>
   <?php  foreach( $array as $list  ): ?> 
     <li><?php echo $list['post_title'] ?>
     <?php if( isset( $list['children'] ) && is_array($list['children']) ): ?>
       <ul> 
          <?php second_level_list( $list['children'] ); ?>
       </ul> 
      <?php endif; ?>  
     </li>
   <?php endforeach; ?>  
</ul>

完成结果https://3v4l.org/YD0Yc

答案 1 :(得分:0)

尝试以下解决方案:

function htmlList($arr)
{
    $list = '<ul>';
    foreach ($arr as $value)
    {
        $listItem = (is_array($value) ? htmlList($value) : $value);
        $list .= "<li>$listItem</li>";
    }
    $list .= '</ul>';
    return $list;
}
echo htmlList($array);

答案 2 :(得分:0)

我调整了你的PHP代码,似乎对我有用:

function second_level_list( $arr ){
  foreach( $arr as $item ){
     echo '<li>'.$item['post_title'].'</li>';
     if( isset($item['children']) && is_array($item['children']) ){
     echo '<ul>';
       second_level_list( $item['children']);
     echo '</ul>';
     }
  }
}

感谢队友