【问题标题】:PHP Turn a multidimensional array into nested listPHP将多维数组转换为嵌套列表
【发布时间】:2016-09-28 09:30:20
【问题描述】:

我从一个 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>

非常感谢帮助!提前致谢

【问题讨论】:

  • SQL 结果结构错误。您有嵌套的孩子,但您想将它们视为同一父母的孩子。怎么了?你的概念,还是数据库结构?
  • 实际上我从 phpMyAdmin 获得了未嵌套的数据。我使用一个函数来构建一个新数组,根据“parent_id”列将孩子嵌套到他们的父母。

标签: php arrays list multidimensional-array nested


【解决方案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

【讨论】:

    【解决方案2】:

    我稍微修改了你的 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>';
         }
      }
    }
    

    谢谢老哥

    【讨论】:

      【解决方案3】:

      尝试以下解决方案:

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

      【讨论】:

      • “make_list”在哪里定义?
      • 我认为回答者的意思是htmlList()。您需要为子列表调用相同的函数。
      • htmlList($array) 而不是 htmlList($arr)
      猜你喜欢
      • 2022-01-09
      • 2010-12-27
      • 1970-01-01
      • 2014-12-30
      • 2018-02-10
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      相关资源
      最近更新 更多