0

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?

5
  • Unclear, what is the problem? Commented Apr 21, 2017 at 6:51
  • I'm sorry . I have make the question more clear . Commented Apr 21, 2017 at 6:56
  • Can you add expected output? Commented Apr 21, 2017 at 7:04
  • I have add expected result Commented Apr 21, 2017 at 7:05
  • Please check answer Commented Apr 21, 2017 at 7:28

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Ashutosh, I have try to create the array ,then do like as you said ,but still cannot get the results that i want . I have add create array code in my question
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.