du13520157325 2017-02-01 01:13
浏览 64
已采纳

PHP将数组拆分为3个独立的UL和列

I am having trouble working out the logic for what I need to achieve

On the web page I have a dynamically generated array from a set of results. The array count can be any number. For example on one page there could be 11 items whereas on another 211.

What I need is to split this into 3 columns and 3 UL's.

Lets say the array holds 23 items, The desired output would be:

    <div class="column">
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
            <li>Item 4</li>
            <li>Item 5</li>
            <li>Item 6</li>
            <li>Item 7</li>
            <li>Item 8</li>
        </ul>
    </div>
    <div class="column">
        <ul>
            <li>Item 9</li>
            <li>Item 10</li>
            <li>Item 11</li>
            <li>Item 12</li>
            <li>Item 13</li>
            <li>Item 14</li>
            <li>Item 15</li>
            <li>Item 16</li>
        </ul>
    </div>
    <div class="column">
        <ul>
            <li>Item 17</li>
            <li>Item 18</li>
            <li>Item 19</li>
            <li>Item 20</li>
            <li>Item 21</li>
            <li>Item 22</li>
            <li>Item 23</li>
        </ul>
    </div>

Where the items are in order, and they are split as equally as possible. If there is an odd number, that is fine as the last column will be less as shown above.

I have tried using the array_chunk()

$ar_list = array("<li>Item 1</li>","<li>Item 2</li>","<li>Item 3</li>","<li>Item 4</li>","<li>Item 5</li>");
$lists = array_chunk($ar_list, 3);



foreach ($lists as $list) {
  echo '<ul>';
  echo $list;
  echo '</ul>';
}

However, this returns:

Array

Array

Array

Array

Array

Can someone please help me get the contents of: $ar_list to output like the desired effect?

Thank you in advance

  • 写回答

2条回答 默认 最新

  • doulin3510 2017-02-01 01:22
    关注

    The $size parameter of the array_chunk function is actually the number of items in each chunk, not the number of chunks. In order to get three, you should first calculate the number of items in each chunk by dividing the size of the array by 3 and then using that count, rounded up, in the array_chunk function. And when you iterate over the array, you need a nested loop to output each list item of each column.

    Like this:

    <?php
    
    $ar_list = array("<li>Item 1</li>","<li>Item 2</li>","<li>Item 3</li>","<li>Item 4</li>","<li>Item 5</li>");
    $rows = ceil(count($ar_list) / 3);
    $lists  = array_chunk($ar_list, $rows);
    
    foreach ( $lists as $column) {
        echo '<ul>';
        foreach ($column as $item) {
            echo $item;
        }
        echo '</ul>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?