切片数组到其他4阵列 [英] Slice an array into 4 other arrays

查看:113
本文介绍了切片数组到其他4阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要在其他4阵列片,因为​​我想显示在四列的第一个数组的内容的数组。

I have an array which I want to slice in 4 other arrays because I want to display the content of the first array on four columns.

我已经尝试了上述code,但我得到的是N列有4项。

I have tried the code above, but what I get is N columns with 4 items.

$groups = array();
for ($i = 0; $i < count($menu); $i += 4) $groups[] = array_slice($menu, $i, 4);

可以这样才能进行修改,以准确地获得4列和分配值,使他们适合?

Can this be modified in order to get exactly 4 columns and distribute the values so they fit?

推荐答案

像迈克尔Berkowski建议:

Like Michael Berkowski suggested:

$groups = array_chunk($menu,4);

应该给你你所需要的。如果你更进的体力劳动的:

$groups = array();
while($groups[] = array_splice($menu,0,4))
{//no need for any code here ^^ chunks the array just fine
    printf('This loop will run another %d times<br/>',(int)ceil(count($menu)/4));
}

更新:

我看到我得到这个有点不对劲......想成块阵列4,不进的四个阵列:

I see I got this a bit wrong... want to chunk into 4 arrays, not into arrays of four:

$groups = array_chunk($menu,(int)ceil(count($menu)/4));

这篇关于切片数组到其他4阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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