使用PHP填充均匀分布的数组矩阵

时间:2010-10-18 20:05:26

标签: php arrays matrix distribution

我有一个包含时间列表的对象数组。我必须将它固定在一个类似Matrix的数组中,以便将它放在一个表中。这是我的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;

这些代表了电影的播放表。从行动here可以看出。这个代码,现在太复杂了(恕我直言),可以简化,只是我不知道如何。也许通过一些Matrix-library?还是我不知道的一些algorythm?

生成的结构应该是一个表示标题的数组(表的列名)和一个表示行的数组。如下:

$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以下) 另请注意,上面给出的输出结构也可能已经过优化,如果有方法可以简化代码,请通过简化或更改生成的“矩阵”来告诉我。

我现在所做的伪代码:

  1. 按标题输入$节点
  2. 遍历所有$节点以查找矩阵的宽度(14:00到17:00)并填充$header
  3. 再次循环遍历所有$节点,对于$ header上的每个节点循环,并使用empties ('')或时间戳填充数组。
  4. 现在,对于上面的三个条目,这不是一个大问题,但是这个矩阵可以变得非常宽(大的$ headers数组)和深(很多$节点),导致大量的重复循环。 但是性能和重复循环不是我最关心的问题,主要是拥有更清晰,嵌套更少,代码更简单的代码。 :)

1 个答案:

答案 0 :(得分:0)

我正在报废你的想法,希望它可以帮助你。

首先以这种格式获取数据:

$movies = array (
    array (
        'title' => 'Babies',
        'times' => array(
                    1286460000,1286467200,1286476200,1286487000
        )
    ),
    array (
        'title' => 'El secreto de sus ojos',
        'times' => array(
                    1286474400,1286481600,1286483400
        )
    )
    // more movies...
);

以下是工作代码的链接:http://ideone.com/P4zZn

这是代码

<?php
date_default_timezone_set('GMT');
$start_hour = 12; // Start hour of matrix
$end_hour = 22; // End hour of matrix
$skip_hours = array( 18, 20 );

// Get the timestamp for each hour in the matrix
foreach( range( $start_hour, $end_hour ) as $hr ) {
    if ( in_array( $hr, $skip_hours ) ) {
        continue;
    }
    $time_sections[] = mktime( $hr,0,0, 10, 7, 2010 ); // You'll want to put null or the month/day to use the current month/day
}

$movies = array (
    array (
        'title' => 'Babies',
        'times' => array(
                    1286460000,1286467200,1286476200,1286487000
        )
    ),
    array (
        'title' => 'El secreto de sus ojos',
        'times' => array(
                    1286474400,1286481600,1286483400
        )
    )
);


?>

<table border="1">
    <thead>
        <th>Movie</th>
        <?php foreach( $time_sections as $time_section ): ?>
        <th><?php echo date( 'G:i', $time_section ) ?> </th>
        <?php endforeach; ?>
    </thead>
    <tbody>
    <?php foreach( $movies as $movie ): ?>
        <tr>
            <td><?php echo $movie['title'] ?></td>
            <?php foreach( $time_sections as $time_section_index => $time_section ): ?>
            <td>
                <!-- while there are movie times left and the first movie time in the array is less than the next timestamp print the movie time -->
                <?php while( count( $movie['times'] ) && current( $movie['times'] ) < $time_sections[$time_section_index + 1] ): ?>
                    <!-- echo the time and pull it from the array -->
                    <?php echo date( 'G:i', array_shift( $movie['times'] ) ) ?> 
                <?php endwhile; ?>
            </td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>
相关问题