0

我从我的选择语句中得到这个:

Array
(
[0] => Array
    (
        [time] => 2013-11-06 09:15:00
        [type] => 1
    )

[1] => Array
    (
        [time] => 2013-11-06 11:00:00
        [type] => 2
    )

[2] => Array
    (
        [time] => 2013-11-06 11:15:00
        [type] => 3
    )

[3] => Array
    (
        [time] => 2013-11-06 13:00:00
        [type] => 4
    )

[4] => Array
    (
        [time] => 2013-11-06 13:29:00
        [type] => 5
    )

[5] => Array
    (
        [time] => 2013-11-06 15:00:00
        [type] => 6
    )

[6] => Array
    (
        [time] => 2013-11-06 15:15:00
        [type] => 7
    )

[7] => Array
    (
        [time] => 2013-11-06 17:00:00
        [type] => 8
    )

[8] => Array
    (
        [time] => 2013-11-07 09:00:00
        [type] => 1
    )

[9] => Array
    (
        [time] => 2013-11-07 10:15:00
        [type] => 2
    )

[10] => Array
    (
        [time] => 2013-11-07 10:30:00
        [type] => 3
    )

[11] => Array
    (
        [time] => 2013-11-07 13:00:00
        [type] => 4
    )

[12] => Array
    (
        [time] => 2013-11-07 13:30:00
        [type] => 5
    )

[13] => Array
    (
        [time] => 2013-11-07 14:30:00
        [type] => 6
    )

[14] => Array
    (
        [time] => 2013-11-07 14:45:00
        [type] => 7
    )

[15] => Array
    (
        [time] => 2013-11-07 17:00:00
        [type] => 8
    )

)

我想要做的是按日期对数组进行分组并将每个时间值设置为该数组的一个元素,但是将键从 [time] 更改为分配给 [type] 的数值。

所以我会得到这样的东西:

$records[] = array();

$records[2013-11-06] => Array
    ( [1] => 09:15:00
      [2] => 11:15:00 
      [3] => 11:15:00 
      etc.
    )
$records[2013-11-07] => Array
    ( [1] => 09:00:00
      [2] => 10:15:00 
      [3] => 10:30:00
      etc.
    )

非常感谢任何帮助,因为我被困住了!谢谢!

4

3 回答 3

1

假设您使用的是 MySQL,您可以像这样重写您的查询:

  SELECT DATE(time) AS date, GROUP_CONCAT(TIME(time) ORDER BY time) AS time
    FROM table_name
GROUP BY date

你会得到这样的数据:

|------------|------------------------------------ --------------------------------------|
| 日期 | 时间 |
|------------|------------------------------------ --------------------------------------|
| 2013-11-06 | 09:15:00,11:00:00,11:15:00,13:00:00,13:29:00,15:00:00,15:15:00,17:00:00 |
| 2013-11-07 | 09:00:00,10:15:00,10:30:00,13:00:00,13:30:00,14:30:00,14:45:00,17:00:00 |
|------------|------------------------------------ --------------------------------------|

从中您可以简单地使用 PHP explode()函数将每条记录的所有时间值作为数组获取。

于 2013-11-07T23:57:09.683 回答
0

您可以迭代您的行并分配所需的值。

$records = array();
foreach ($rows as $row) {
    $timestamp = strtotime($row['time']);
    $date = date("Y-m-d", $timestamp);
    $time = date("H:i:s", $timestamp);
    if (!is_array($records[$date])) $records[$date] = array();
    $records[$date][ $row['type'] ] = $time;
}
于 2013-11-07T23:48:03.453 回答
0

尝试这个:

$records = array();
foreach($yourarray as $arr)
  {
  list($date,$time)=explode(' ',$arr['time']);
  $records[$date][$arr['type']]=$time;
  }
于 2013-11-07T23:46:26.707 回答