如何更正我的数组以创建我想要的表输出?

时间:2015-02-25 14:23:25

标签: php arrays multidimensional-array

我有两个数组,一个用于在类似于结构的表中创建第一列(服务),另一个用于在此之后的每个其他列(日)。第一列数组具有以下结构:

Array
(
    [0] => DPW
    [1] => PNS
    [2] => WS1
    [3] => WS2
)

这是一个动态数组,因此可能会有更多的值,但它将始终是唯一的并按字母顺序排序。

我对其他列的第二个数组具有以下结构:

Array
(
    [0] => Array
        (
            [Monday] => Array
                (
                    [0] => Array
                        (
                            [date] => 42016
                            [message] => 07:00 START
                            [service] => DPW

                        )

                    [1] => Array
                        (
                            [date] => 42016
                            [message] => 16:30 START
                            [service] => PNS
                        )

                    [2] => Array
                        (
                            [date] => 42016
                            [message] => 
                            [service] => WS1
                        )

    // etc ...


    [3] => Array
        (
            [Thursday] => Array
                (
                    [0] => Array
                        (
                            [date] => 42017
                            [message] =>
                            [service] => DPW
                        )

                    [1] => Array
                        (
                            [date] => 42017
                            [message] => 16:00 CUT OFF
                            [service] => DPW
                        )

                    [2] => Array
                        (
                            [date] => 42017
                            [message] =>
                            [service] => PNS
                        )
                )
        )
)

所以基本上每一天都是下一栏。我的问题是我最初认为第二个数组中的服务密钥每天总是以相同的顺序,但事实并非如此。你可以从周四看到DPW是连续两次,而周一不是。我可以改变这些数组的格式,所以这就是我的问题所在。

View desired output

我的想法是,对于初学者来说应该包含与服务数组相同数量的键,那么我应该添加一个空值来组成相同数量的键吗?为了满足连续的相同服务,我应该添加更多级别来满足多个值,如下所示:

Array
(

    // etc ...


    [3] => Array
        (
            [Thursday] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [date] => 42017
                                    [message] =>
                                    [service] => DPW
                                 )

                            [1] => Array
                                (
                                    [date] => 42017
                                    [message] =>
                                    [service] => DPW
                                 )
                        )

                    [2] => Array
                        (
                            [0] => Array
                                (
                                    [date] => 42017
                                    [message] =>
                                    [service] => PNS
                                 )
                        )

                    [3] => Array
                        (

                        )

                    [4] => Array
                        (

                        )
                )
        )
)

我还添加了空白值。最后,我将如何以与第一个(服务)数组相同的顺序打印这些值,以便它们不在错误的服务之下?

如果很难解释这一点,我可能会以错误的方式解决这个问题,但这就是我提出问题的原因。



修改

几乎就是这个,我现在唯一的问题是我需要将开始和截止日期显示为多天内展开的同一项目。例如,数组可能在某些时候具有这些值:

[1] => Array
    (
        [date] => 42016
        [message] => 16:30 START
        [service] => DPM
    )

// etc...

[3] => Array
    (
        [date] => 42021
        [message] => 16:30 CUT OFF
        [service] => DPM
    )

因此,我需要一种方法将这些在自己的行上用背景颜色或其他东西来显示它们是一个项目,如上图所示。不确定这是否可行?

由于 罗伯特!

1 个答案:

答案 0 :(得分:1)

我不确定这正是你想要的,但它可能有所帮助:

$services = array('DPW', 'PNS', 'WS1', 'WS2');
$days = array(
    array(
        'Monday' => array(
                    array(
                            'date' => 42016,
                            'message' => '07:00 START',
                            'service' => 'DPW'

                        ),
                    array(
                            'date' => 42016,
                            'message' => '16:30 START',
                            'service' => 'PNS'
                        ),
                    array(
                            'date' => 42016,
                            'message' => '',
                            'service' => 'WS1'
                        ),
                ),
    ),

    array(
            'Thursday' => array(
                    array(
                            'date' => 42017,
                            'message' => '',
                            'service' => 'DPW'
                        ),
                    array(
                            'date' => 42017,
                            'message' => '16:00 CUT OFF',
                            'service' => 'DPW'
                        ),
                    array(
                            'date' => 42017,
                            'message' => '',
                            'service' => 'PNS'
                        )
                )
        )
    );

这是HTML表格:

<?php $service_days = []; ?>
<table cellpadding="10" width="100%" align="left">
    <thead>
        <td style="background:#333;color:#FFF">Service</td>
        <?php foreach($days as $day): ?>
            <td style="background:#666;color:#FFF"><?php echo array_keys($day)[0]; ?></td>
            <?php 
              foreach(array_values($day)[0] as $d)  
                   $service_days[array_keys($day)[0]][$d['service']][] = $d;
           ?>
        <?php endforeach; ?>
    </thead>

    <?php foreach($services as $service):  ?>
        <tr>
            <td><?php echo $service ?></td>     

            <?php foreach($service_days as $day => $srv): ?>
                <td>    
                <?php foreach($srv as $_service => $periode): ?>

                    <?php if($_service == $service): ?>

                            <?php foreach($periode as $p): ?>
                                <p style="background:#DDD"><?php echo $p['date'], '<br>', $p['message']; ?></p>
                            <?php endforeach; ?>

                    <?php endif; ?>

                <?php endforeach; ?>
                &nbsp;</td> 
            <?php endforeach; ?>

        </tr>
    <?php endforeach; ?>    
</table>