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 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式