(PHP)S $横跨固定数量的列的对$ pading数组的内容同样 [英] (PHP) Spreading array contents equally across a fixed number of columns

查看:147
本文介绍了(PHP)S $横跨固定数量的列的对$ pading数组的内容同样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在这里坚果 - 希望你能帮助我弄清楚了这一点!什么应该是很简单的数学是完全混淆了我。

I am going nuts here - hope you can help me figure this out! What is supposed to be very simple math is utterly confusing me.

$columns = 3;
$items = range(1,20);
$total = count($items);

$col1 = ???; $col2 = ???; $col3 = ???;

// $col1 must be an array of (1,2,3,4...)
// $col2 must be an array of (8,9,10,11...)
// $col3 must be an array of (15,16,17,18...)

COL1       COL2        COL3
1          8           15
2          9           16
3          10          17
4          11          18
5          12          19
6          13          20
7          14

以上是我试图达到一种视觉的例子。基本上,对于在阵列中并且对于任何给定数量的列,该怎么办项中任何给定数目的我产生如长度相等尽可能阵列(等于列数)的n个。如果长度相等是不可能的(如在上面的例子中),他们必须为s $ P $垫出尽可能均匀和最后一个阵列必须是最短的。

The above is a visual example of what I am trying to achieve. Basically, for any given number of items in an array and for any given number of columns, how do I produce n number of arrays (equalling number of columns) that are as equal length as possible. If equal length is not possible (as in the example above), they must be spread out as evenly as possible and the last array must be the shortest.

我如何构建$ COL1 / 2/3在本例中的任何指导,上面会多AP preciated!

Any guidance on how I construct $col1/2/3 in the example above would be much appreciated!

请忽略我使用范围和整数生成阵列的事实 - 这仅仅是为了简化例子。假设数组将包含字符串。

Please ignore the fact that I used range and integers to generate the array - this is just to simplify the example. Assume the array will contain strings.

感谢您的帮助!

推荐答案

您想使用 array_chunk ,但你需要计算块大小自己:

You do want to use array_chunk, but you need to calculate the chunk size yourself:

list($col1, $col2, $col3) = array_chunk($items, ceil($total / $columns));

这篇关于(PHP)S $横跨固定数量的列的对$ pading数组的内容同样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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