如何从嵌套数组中制作表格数据

时间:2011-05-23 12:28:34

标签: php arrays nested

我正在研究出勤报告,我们有数组:

Array
(
    [0] => Array
        (
            [ActatekLog] => Array
                (
                    [id] => 1
                    [timeentry] => 2011-02-16 00:09:02
                    [eventID] => OUT
                    [terminalSN] => 00111DA08C8B
                    [jpegPhoto] => 
                )

        )

    [1] => Array
        (
            [ActatekLog] => Array
                (
                    [id] => 1
                    [timeentry] => 2011-02-16 00:09:12
                    [eventID] => IN
                    [terminalSN] => 00111DA08C8B
                    [jpegPhoto] => 
                )

        )

    [2] => Array
        (
            [ActatekLog] => Array
                (
                    [id] => 1
                    [timeentry] => 2011-02-16 00:11:31
                    [eventID] => OUT
                    [terminalSN] => 00111DA08C8B
                    [jpegPhoto] => 
                )

        )

    [3] => Array
        (
            [ActatekLog] => Array
                (
                    [id] => 1
                    [timeentry] => 2011-02-16 00:11:40
                    [eventID] => IN
                    [terminalSN] => 00111DA08C8B
                    [jpegPhoto] => 
                )

        )

    [4] => Array
        (
            [ActatekLog] => Array
                (
                    [id] => 1
                    [timeentry] => 2011-02-16 00:13:17
                    [eventID] => OUT
                    [terminalSN] => 00111DA08C8B
                    [jpegPhoto] => 
                )

        )

    [5] => Array
        (
            [ActatekLog] => Array
                (
                    [id] => 1
                    [timeentry] => 2011-02-16 00:13:21
                    [eventID] => IN
                    [terminalSN] => 00111DA08C8B
                    [jpegPhoto] => 
                )

        )

)

报告格式为:

Date       In Time     Out Time    Work Time
2011-02-16

我需要在同一天的一行中及时显示帮助。

请帮忙

3 个答案:

答案 0 :(得分:1)

我首先要先对输出数组进行格式化。

$arr = array();
foreach ( $result as $row ) {
    $arr[ $row[ 'terminalSN' ] ][ $row[ 'eventID' ] ] = $row;
}

然后输出应该更容易一些:

<table>
    <tr>
        <th>Date</th>
        <th>Time In</th>
        <th>Time Out</th>
        <th>Work Time</th>
    </tr>
    <?php foreach ( $arr as $key => $value ) : ?>
        <tr>
            <td><?php echo date( 'Y-m-d', strtotime( $value[ 'IN' ][ 'timeentry' ] ) ); ?></td>
            <td><?php echo date( 'H:i:s', strtotime( $value[ 'IN' ][ 'timeentry' ] ) ); ?></td>
            <td><?php echo date( 'H:i:s', strtotime( $value[ 'OUT' ][ 'timeentry' ] ) ); ?></td>
            <td><?php echo "Calculate based on out minus in. Pretty simple at this point." ?></td>
        </tr>
    <?php endforeach; ?>
</table>

答案 1 :(得分:0)

虽然你的数组中没有两个你在表中声明的值,但你可以这样做。

<table>
    <tr>
        <th>Date</th>
        <th>Time In</th>
        <th>Time Out</th>
        <th>Work Time</th>
    </tr>
<?php foreach ($thisArr as $arr) : ?>
    <tr>
        <td><?php echo date('Y-m-d'); ?></td>
        <td><?php echo $arr['ActatekLog']['timeentry']; ?></td>
        <td><?php echo "no time out in the array??"; ?></td>
        <td><?php echo "you need to calculate this I assume"; ?></td>
    </tr>
<?php endforeach; ?>
</table>

答案 2 :(得分:0)

我正在使用您的数据模型永远不会更改,并且您的数据已经按照您希望的方式进行排序。所以,对于这样的数组:

$data = array
(
    0 => array
    (
        'ActateLog' => array
        (
            'id' => 1,
            'timeentry' => '2011-02-16 00:09:02',
            'eventID' => 'OUT',
            'terminalSN' => '00111DA08C8B',
            'jpegPhoto' => false
        )
    ),
    1 => array
    (
        'ActateLog' => array
        (
            'id' => 1,
            'timeentry' => '2011-02-16 00:09:12',
            'eventID' => 'IN',
            'terminalSN' => '00111DA08C8B',
            'jpegPhoto' => false
        )
    )
);

我创建了一个函数,它返回一个包含n / 2行的数组,并且在同一行中同时包含IN和OUT。

function processMyArray($data)
{
    $output = array();
    $lim = count($data);
    $i = $j = 0;

    for($i=0; $i<$lim; $i+=2, $j++)
    {
        $output[$j] = array
        (
            'date' => substr($data[$i]['ActateLog']['timeentry'], 0, 10),
            'IN' => substr($data[$i]['ActateLog']['timeentry'], 11),
            'OUT' => substr($data[$i+1]['ActateLog']['timeentry'], 11),
            'work_time' => strtotime($data[$i+1]['ActateLog']['timeentry']) - strtotime($data[$i]['ActateLog']['timeentry'])
        );
    }

    return $output;
}