使用 PHP 填充均匀分布的数组矩阵
我有一个对象数组,其中包含时间列表。我必须将其放入类似矩阵的数组中,以便将其放入表格中。这是我的之前关于如何构建日历表的问题
$node->sessions = array(
1286452800, // '2010-10-07 14:00:00'
1286460000, // '2010-10-07 16:00:00'
);
$node->title = 'Foo';
$nodes[1] = $node;
$node->sessions = array(
1286452800, // '2010-10-07 14:00:00'
1286460000, // '2010-10-07 16:00:00'
1286461800, // '2010-10-07 16:30:00'
);
$node->title = 'Bar';
$nodes[2] = $node;
$node->sessions = array(
1286460000, // '2010-10-07 16:00:00'
1286461800, // '2010-10-07 16:30:00'
1286465400, // '2010-10-07 17:30:00'
);
$node->title = 'Baz';
$nodes[3] = $node;
这些代表电影播放日期的表。正如此处的实际操作所示。目前的代码过于复杂(恕我直言),可以简化,只是我不知道如何简化。也许通过一些矩阵库?或者我不知道的一些算法?
生成的结构应该是一个表示标题(表的列名)的数组和一个表示行的数组。如下:
$header = array(
' ', // empty "cell"
ttt, // ttt being the timestamp for '14:00'
uuu, // idem for '15:00'
vvv, // idem for '16:00'
www, // idem '17:00'
);
// title, 14:00, 15:00, 16:00, 17:00
$rows = array(
array(
'Bar', 1286452800, '', array(1286460000, 1286461800), '', //Both 16:00 and 16:30 grouped under 16:00 'header'
),
array(
'Baz', '', '', array(1286460000, 1286461800), 1286465400
),
array(
'Foo', 1286452800, '', 1286460000, '',
)
);
每行应具有相同数量的“单元格”。 时间戳应按其所属时间分组(16:15、16:30 等均分组在 16:00 下) 另请注意,上面给出的输出结构也可能会被优化,请告诉我是否有办法通过简化或更改生成的“矩阵”来简化代码。
我现在所做的伪代码:
- 对所有$nodes 按标题
- 循环排序 $nodes 以查找矩阵的宽度(14:00 到 17:00),并
- 在所有 $nodes 上填充
$header
循环再次 $nodes,对于每个节点,循环遍历 $header 并用空('')
或时间戳填充数组。
现在,对于上面的三个条目,这不是一个大问题,但是这个矩阵可能会变得非常宽(大 $headers 数组)和深(许多 $nodes),从而导致大量重复循环。 但性能和重复循环并不是我最关心的,主要是拥有更干净、更少嵌套、更简单的代码。 :)
I have an array of objects, containing lists of times. I have to stick this in a Matrix-alike array, in order to put it in a table. This is a part of my earlier SO question on how to build calendar tables
$node->sessions = array(
1286452800, // '2010-10-07 14:00:00'
1286460000, // '2010-10-07 16:00:00'
);
$node->title = 'Foo';
$nodes[1] = $node;
$node->sessions = array(
1286452800, // '2010-10-07 14:00:00'
1286460000, // '2010-10-07 16:00:00'
1286461800, // '2010-10-07 16:30:00'
);
$node->title = 'Bar';
$nodes[2] = $node;
$node->sessions = array(
1286460000, // '2010-10-07 16:00:00'
1286461800, // '2010-10-07 16:30:00'
1286465400, // '2010-10-07 17:30:00'
);
$node->title = 'Baz';
$nodes[3] = $node;
These represent a table of playdates of movies. As can be seen in action here. The code for this, right now is far too complex (IMHO) and could be simplified, just that I don't know how. Maybe trough some Matrix-library? Or some algorythm that I am unaware of?
The resulting structure, should be an array representing the header (column names of the table) and an array-with-arrays representing the rows. As follows:
$header = array(
' ', // empty "cell"
ttt, // ttt being the timestamp for '14:00'
uuu, // idem for '15:00'
vvv, // idem for '16:00'
www, // idem '17:00'
);
// title, 14:00, 15:00, 16:00, 17:00
$rows = array(
array(
'Bar', 1286452800, '', array(1286460000, 1286461800), '', //Both 16:00 and 16:30 grouped under 16:00 'header'
),
array(
'Baz', '', '', array(1286460000, 1286461800), 1286465400
),
array(
'Foo', 1286452800, '', 1286460000, '',
)
);
Each row should have the same amount of "cells".
Timestamps should be grouped under the hour they belong to (16:15, 16:30 etc all grouped under 16:00)
Also note that the above-given output structure might be optimised too, please let me know if there are ways to simplify the code, by simplifying or changing the resulting "matrix".
Pseudocode for what I do now:
- usort $nodes by title
- loop over all the $nodes to find the width of the matrix (14:00 to 17:00) and fill the
$header
- loop over all the $nodes again, for each node loop over $header and fill an array with either empties
('')
or with the timestamps.
Now, for a the three entries above, that is not a big problem, but this matrix can grow very wide (large $headers array) and deep (many $nodes) resulting in a lot of duplicate looping.
But performance and duplicate looping is not my greatest concern, mostly having cleaner and less nested, less complex code, is. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在放弃你的想法,并想出我会如何做,希望它可以帮助你。
首先以这种格式获取数据:
这是工作代码的链接:http://ideone.com/P4zZn
这里是代码
I'm scrapping your and coming up with how i would do it in hopes it may help you.
First get the data in this format:
Here's a link to working code: http://ideone.com/P4zZn
Here's the code