【问题标题】:Sorting out rows with same timestamp排序具有相同时间戳的行
【发布时间】:2016-05-18 05:30:51
【问题描述】:

我有一个只有键、值和时间戳的 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。

感谢您的帮助:)

【问题讨论】:

  • 我有点困惑。您在上面提供的数组是您的表单发送的 POST 请求吗?你想把它存储到mysql中吗?要么.. ?你能解释得更好吗?

标签: php mysql sql arrays timestamp


【解决方案1】:

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

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

希望这能给你一个起点。

【讨论】:

    【解决方案2】:

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

    $last = 0;
    foreach($data as $row) {
        if ($last != $row['timestamp'])
            echo "\r\n";
    
        $last = $row['timestamp'];
        echo $row['key'] . ':' . $row['value'] . ' ';
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-18
      • 1970-01-01
      • 2016-12-27
      • 2020-07-30
      • 1970-01-01
      • 2016-10-14
      • 2023-03-23
      • 2020-07-07
      • 1970-01-01
      相关资源
      最近更新 更多