dtp0760 2015-06-15 12:19
浏览 36
已采纳

如何使用array_chunk并定义每个块的大小?

I am counting the number of results in each array first and then print them like this:

if ($array_count == '12'){

    $output = '<div class="row">';
    $cat_array = array_chunk((array)$cat_array, 3);
    foreach($cat_array as $column) {
        $output .= '<div class="col-xs-3">';
        foreach ($column as $row) {
           $output .= '<div class="row"><div class="col-xs-12"><small><a href="' . 
                      $row['subcat_link'] . '" class="cat_filter_link">' . $row['name'] . '</a></small></div></div>';
        }
        $output .= '</div>';
    }
    $output .= '</div>';

    }

In that case, I am putting 3 into each chunk. Is there any easy way to define, if possible, that I want 4 chunks of 5,3,2,2?

  • 写回答

1条回答 默认 最新

  • duanbianweng5353 2015-06-15 12:43
    关注

    You can use array_slice as

    $arr = array(1,2,3,4,5,6,7,8,9,10,11,12);
    $result[0] = array_slice($arr, 0,5);
    $result[1] = array_slice($arr, 5,3);
    $result[2] = array_slice($arr, 8,2);
    $result[3] = array_slice($arr, 10,2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改