排序具有相同时间戳的行

时间:2016-02-08 14:05:05

标签: php mysql sql arrays timestamp

我有mysql表,其中只包含密钥,值和时间戳。数据来自一个表单,每个from字段都添加一个键值对,所有这些都具有相同的时间戳。现在他们就这样出来了:



 [0] => Array
        (
            [key] => name
            [value] => john
            [timestamp] => 2016-02-08 14:16:00
        )

    [1] => Array
        (
            [key] => email
            [value] => john$john.com
            [timestamp] => 2016-02-08 14:16:00
        )

    [2] => Array
        (
            [key] => name
            [value] => guru
            [timestamp] => 2016-02-08 14:16:05
        )

    [3] => Array
        (
            [key] => email
            [value] => test@gmail.com
            [timestamp] => 2016-02-08 14:16:05
        )




我希望它能够使用相同时间戳的键值对并将它们打印在一行中,然后打印到下一行中的下一个时间戳。我正在使用php。

感谢您的帮助:)

2 个答案:

答案 0 :(得分:0)

您需要存储时间戳以在下一次传递时进行检查。

$previous = null;
foreach ($array AS $item) {
    if ($previous == $item['timestamp']) {
        // Do this on the same row as we already have one like this.
    } else {
        // Do this on a new row as it's the first of its kind.
    }

    // Save the timestmap to use on the next pass of the loop.
    $previous = $item['timestamp'];
}

希望这能为你提供一个从哪里开始。

答案 1 :(得分:0)

你的问题不太清楚,但我认为这是你想要获得的。只需使用foreach

即可
$last = 0;
foreach($data as $row) {
    if ($last != $row['timestamp'])
        echo "\r\n";

    $last = $row['timestamp'];
    echo $row['key'] . ':' . $row['value'] . ' ';
}