如何从嵌套数组中生成表格数据

发布于 2024-11-09 08:08:29 字数 2226 浏览 3 评论 0原文

您好,我正在处理出勤报告,我们有数组中的数据:

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

我需要帮助显示同一天的一行中的时间和外出时间。

请帮忙

Hi iam working on attendance report, data in array we have :

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] => 
                )

        )

)

report format is :

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

i need help in display in time and out time in one row for same day.

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

要走就滚别墨迹 2024-11-16 08:08:29

我首先会稍微格式化输出数组。

$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>

I would start by formatting the output array a little bit first.

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

Then output should be a little easier:

<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>
川水往事 2024-11-16 08:08:29

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

$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;
}

I'm using your data model never changes and that your data is already sorted the way you'd like it to be sorted. So, for an array like this:

$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
        )
    )
);

I made a function, that returns an array with n/2 rows and has both IN and OUT in the same row.

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;
}
无畏 2024-11-16 08:08:29

尽管数组中没有您在表中声明的两个值,但您可以做到这一点。

<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>

Here you go that'll do it though you dont have two values in your array that you state you want in your table.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文