【问题标题】:Group Multi-Dimensional Array and Reassign Keys对多维数组进行分组并重新分配键
【发布时间】:2013-11-07 23:34:41
【问题描述】:

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

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

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

【问题讨论】:

  • 请告诉您您使用的是哪个数据库管理系统 (DBMS)。有时您可以解决重写查询的问题。 :)
  • 这很简单。试试吧。
  • 您也应该包括您尝试过(但失败)的所有尝试

标签: php arrays


【解决方案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() 函数将每条记录的所有时间值作为数组获取。

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

      您可以遍历行并分配所需的值。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 1970-01-01
        • 1970-01-01
        • 2021-12-01
        • 2011-05-31
        • 2021-02-09
        相关资源
        最近更新 更多