MongoDB php Parsing to Json Style Output

Iam currently reading the data from MongoDB using PHP and the following PHP ouput from my program as shown below needs to be converted JSON style output as 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

Answers


You will need to loop through the array and rebuild it the way you need it before encoding:

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

$json = json_encode($data_to_encode);

If you want less variables:

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

$json = json_encode($data);

$json_output = json_encode($MongoDBPHPoutput, true);

Need Your Help

dual boot to Xen cloud platform and windows

windows cloud boot xen

In my system I have windows on one of my hard driver and I have installed Xen cloud platform on the other one. The installation is complete now but I don't know how can I load Xen after reboot? In ...

Why doesn't JSFiddle allows the creation of the playable tone?

javascript jsfiddle

<button onmousedown="play(audio2);">Sine wave</button>