MongoDB php Parsing to Json Style Output -


iam reading data mongodb using php , following php ouput program shown below needs converted json style output shown below

mongo db php output

array ( [_id] => mongoid object ( [$id] => 53481398834269e8e25fa268 ) [category] => 2014-04-01 [value1] => 400 [value2] => 200 ) array ( [_id] => mongoid object ( [$id] => 534813a9834269e8e25fa269 ) [category] => 2014-04-02 [value1] => 405 [value2] => 205 ) array ( [_id] => mongoid object ( [$id] => 534813d7834269e8e25fa26a ) [category] => 2014-04-03 [value1] => 408 [value2] => 211 ) array ( [_id] => mongoid object ( [$id] => 534813ec834269e8e25fa26b ) [category] => 2014-04-04 [value1] => 418 [value2] => 198 ) array ( [_id] => mongoid object ( [$id] => 53481402834269e8e25fa26c ) [category] => 2014-04-05 [value1] => 370 [value2] => 221 ) array ( [_id] => mongoid object ( [$id] => 53481439834269e8e25fa26d ) [category] => 2014-04-06 [value1] => 299 [value2] => 180 ) array ( [_id] => mongoid object ( [$id] => 5348144e834269e8e25fa26e ) [category] => 2014-04-07 [value1] => 311 [value2] => 224 ) array ( [_id] => mongoid object ( [$id] => 53481461834269e8e25fa26f ) [category] => 2014-04-08 [value1] => 315 [value2] => 254 ) array ( [_id] => mongoid object ( [$id] => 5348146f834269e8e25fa270 ) [category] => 2014-04-09 [value1] => 325 [value2] => 264 ) array ( [_id] => mongoid object ( [$id] => 5348147f834269e8e25fa271 ) [category] => 2014-04-10 [value1] => 335 [value2] => 255 ) array ( [_id] => mongoid object ( [$id] => 5348148c834269e8e25fa272 ) [category] => 2014-04-11 [value1] => 365 [value2] => 265 )

json style php out

[ { "category": "2013-08-24", "value1": 417, "value2": 127 }, { "category": "2013-08-25", "value1": 417, "value2": 356 }, { "category": "2013-08-26", "value1": 531, "value2": 585 }, { "category": "2013-08-27", "value1": 333, "value2": 910 }, { "category": "2013-08-28", "value1": 552, "value2": 30 }, { "category": "2013-08-29", "value1": 492, "value2": 371 }, { "category": "2013-08-30", "value1": 379, "value2": 781 }, { "category": "2013-08-31", "value1": 767, "value2": 494 }, { "category": "2013-09-01", "value1": 169, "value2": 364 }, { "category": "2013-09-02", "value1": 314, "value2": 476 }, { "category": "2013-09-03", "value1": 437, "value2": 759 } ]

requires expert help

you need loop through array , rebuild way need before encoding:

$data_to_encode = array(); foreach($mongo_objects $mongo){     $data['category'] = $mongo['category'];     $data['value1'] = $mongo['value1'];     $data['value2'] = $mongo['value2'];     $data_to_encode[] = $data; }  $json = json_encode($data_to_encode); 

if want less variables:

$data = array(); foreach($mongo_objects $mongo){     $data[] = array('category'=>$mongo['category'],                     'value1'=>$mongo['value1'],                     'value2'=>$mongo['value2'],               ); }  $json = json_encode($data); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -