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

创建一个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);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanjue9296 2017-04-21 07:40

    This could be in Javascript

    You can use UnderscoreJS library for this refer http://underscorejs.org/#groupBy

    You can do like this

    "var a = [ { 'DateTime' :'2017-02-01 08:00:23', 'ip' :'10.10.10.1', 'Val' : '1' },

         {
        'DateTime' :'2017-02-01 09:01:23',
        'ip'       :'10.10.10.2',
        'Val'      : '2'
         },
    
         {
        'DateTime' :'2017-02-02 09:01:23',
        'ip'       :'10.10.10.3',
        'Val'      : '3'
        },
    
         {
        'DateTime' :'2017-02-02 13:03:23',
        'ip'       :'10.10.10.4',
        'Val'      : '4'
        },
    
         {
        'DateTime' :'2017-02-03 15:04:23',
        'ip'       :'10.10.10.5',
        'Val'      : '5'
    
        },
    
         {
        'DateTime' :'2017-02-03 20:05:23',
        'ip'       :'10.10.10.6',
        'Val'      : '6'
        }];
    

    Then user _.groupBy function like this

    _.groupBy(a, function(obj){ return new Date(obj.DateTime).toDateString() });"

    Hope it will help you

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题