在php中将数组拆分成碎片 [英] Split an array up into pieces in php

查看:60
本文介绍了在php中将数组拆分成碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您具有以下数组:

[1, 2, 3, 4, 5, 6, 7]

我将如何在php中拆分它,以便获得:

How would I split this up in php so that I have:

[[1, 2, 3], [4, 5, 6] [7]] // divide by three.

我知道会是array % 3之类的东西,但我实际上不确定...

I know it would be something like array % 3 but i'm not actually sure ...

想法?

推荐答案

尝试使用特殊的php函数 array_chunk()(

Try to use a special php function array_chunk() (learn more)

 <?php
      $cars = array("Volvo", "BMW", "Toyota", "Honda", "Mercedes", "Opel");
      // second argument defines how many chunks you gonna make
      print_r(array_chunk($cars, 3));
 ?>  

这篇关于在php中将数组拆分成碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆