Monday, 15 March 2010

php - How to format json to what I need? -



php - How to format json to what I need? -

ok, going out of way right now. suck @ php. building angular app going populate mobile app info database. having pull database fine need json formatted in special way , have no thought how it.

using json_encode how coming database:

[ { "id":"1", "date":"2014-10-03", "time":"2014-10-03 10:45:05", "amount":"5" }, { "id":"2", "date":"2014-10-03", "time":"2014-10-03 12:21:05", "amount":"2" } ]

this how need organized (this dummy info im using in on angular side)

[ { date: '2014-09-04', feeding: [ { id: '1', time: '1409852728000', amount: '3' }, { id: '2', time: '1409874328000', amount: '4' }, ] }, { date: '2014-09-05', feeding: [ { id: '3', time: '1409915908000', amount: '3.5' }, { id: '4', time: '1409957908000', amount: '5' }, ] }, ]

i needs seperated out , grouped date. how go doing this?

airtech there. little update function though. feeding value needs array of objects rather object. need force individual objects array.

function dateparse($in) { $in = json_decode($in); $out = array(); ($i = 0; $i < sizeof($in); $i++) { $date = $in[$i]->date; $isfound = false; ($i2 = 0; $i2 < sizeof($out); $i2++) { if ($date == $out[$i2]["date"]) { // we've run search value before // add together the elements $isfound = true; $out[$i2]["feeding"][] = array( "id" => $in[$i]->id, "time" => $in[$i]->time, "amount" => $in[$i]->amount); break; } } if (!$isfound) { // no matches yet // need add together 1 array $feeding = array("id" => $in[$i]->id, "time" => $in[$i]->time, "amount" => $in[$i]->amount); $out[] = array("date" => $in[$i]->date, "feeding" => array($feeding)); } } homecoming json_encode($out); }

php json formatting

No comments:

Post a Comment