dqrsceg6279196 2017-04-21 06:48
浏览 122
已采纳

创建一个JSON数组对象 - 按相同日期分组到一个新数组?

I'm thinking how to group my array by objects with the same date. I have this result from MySQL query(select DateTime ,ip,Val from my table):

  DateTime                 ip                       Val
2017-02-01 08:00:23       10.10.10.1                2
2017-02-01 09:01:23       10.10.10.2                3
2017-02-01 12:02:23       10.10.10.3                4
2017-02-02 13:03:23       10.10.10.4                5
2017-02-03 15:04:23       10.10.10.5                6
2017-02-03 20:05:23       10.10.10.6                7

from mysql query result to create json array object as below ,

$sql = "select DateTime ,ip,Val from my table order by DateTime ASC ;";
$result = $db->query($sql);
$data = array();
$rowary = array();
$i=0;
while($row = mysqli_fetch_array($result))
 {
    $rowary['DateTime'] = $row['DateTime '] ;
    $rowary['ip'] = $row['ip'] ;
    $rowary['Val'] = $row['Val'] ;
    $data[$i++]=$rowary;
  }
echo '<pre>' . var_export($data, true) . '</pre>';

Is it possible to create a JSON array object that group by the same date If I use

var data = <?php echo json_encode($Data, JSON_PRETTY_PRINT);?> 

in javascript,

expected result is:

 [
    [
      {
         "DateTime": "2017-02-01 08:00:23",
         "ip": "10.10.10.1",
         "Val": "2"
      },
      {
         "DateTime": "2017-02-01 09:01:23",
         "ip": "10.10.10.2",
         "Val": "3"
      },
      {
         "DateTime": "2017-02-01 12:02:23",
         "ip": "10.10.10.3",
         "Val": "4"
      }
   ],
   [
     {
        "DateTime": "2017-02-02 13:03:23",
        "ip": "10.10.10.4",
        "Val": "5"
     }
   ],
   [
    {
       "DateTime": "2017-02-03 15:04:23",
       "ip": "10.10.10.5",
       "Val": "6"
    },
    {
       "DateTime": "2017-02-03 20:05:23",
       "ip": "10.10.10.6",
       "Val": "7"
    }
   ]

 ]

How can I create a JSON array object that group by the same date?

  • 写回答

2条回答 默认 最新

  • doucuo4413 2017-04-21 07:06
    关注

    Get data from mySQL in asc or dsc order, Then do like this:-

    $a = array(
        array(
            'DateTime' => '2017-02-01 08:00:23',
            'ip'       => '10.10.10.1',
            'Val'       => '1'
        ),
        array(
            'DateTime' => '2017-02-01 09:01:23',
            'ip'       => '10.10.10.2',
            'Val'       => '2'
        ),
        array(
            'DateTime' => '2017-02-02 09:01:23',
            'ip'       => '10.10.10.3',
            'Val'       => '3'
        ),
        array(
            'DateTime' => '2017-02-02 13:03:23',
            'ip'       => '10.10.10.4',
            'Val'       => '4'
        ),
        array(
            'DateTime' => '2017-02-03 15:04:23',
            'ip'       => '10.10.10.5',
            'Val'       => '5'
        ),
        array(
            'DateTime' => '2017-02-03 20:05:23',
            'ip'       => '10.10.10.6',
            'Val'       => '6'
        ),
    );
    
    $FirstDate = date('Y-m-d', strtotime($a[0]['DateTime']));
    $output = array();
    $i = 0;
    
    foreach($a as $value){
    
        $currentDate = date('Y-m-d', strtotime($value['DateTime']));
    
        if($currentDate != $FirstDate){
            $i++;
            $FirstDate = $currentDate;
        }       
    
        $output[$i][] = $value;
    
    }
    
    var_dump($output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里