组多维数组和重新分配键

时间:2013-11-07 23:34:41

标签: php arrays

我从我的选择声明中得到了这个:

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

任何帮助都非常感谢,因为我很难受!谢谢!

3 个答案:

答案 0 :(得分:1)

假设你正在使用MySQL,你可以像这样重写你的查询:

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

你会得到这样的数据:

|------------|-------------------------------------------------------------------------|
| date       | time                                                                    |
|------------|-------------------------------------------------------------------------|
| 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()函数将每条记录的所有时间值作为数组。

答案 1 :(得分:0)

试试这个:

$records = array();
foreach($yourarray as $arr)
  {
  list($date,$time)=explode(' ',$arr['time']);
  $records[$date][$arr['type']]=$time;
  }

答案 2 :(得分: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;
}