php - group elements inside a json -
i'm trying make json looks this:
{ "data": { "error_message": "", "data": { "person": [ [ { "name": "teset1", "image": "http://test.co.il/test/icons/billadress.png" }, { "name": "test2", "image": "http://test.co.il/test/icons/email.png" }, { "name": "test3", "image": "http://test.co.il/test/icons/homeicon.png" } ], [ { "name": "a", "image": "http://test.co.il/shay/keykit/icons/billadress.png" }, { "name": "b", "image": "http://test.co.il/shay/keykit/icons/email.png" }, { "name": "c", "image": "http://dvns.co.il/shay/keykit/icons/homeicon.png" } ], [ { "name": "a", "image": "http://test.co.il/test/icons/billadress.png" }, { "name": "b", "image": "http://test.co.il/test/icons/email.png" }, { "name": "c", "image": "http://dvns.co.il/test/icons/homeicon.png" } ], ] } }, }
this code i'm using after i'm getting name & image mysql query:
$response = $this->_db->query($query)->result_array(); $icons_results = array(); $obj = new stdclass(); foreach ($response $key => $response) { $icons_model = new icons_model(); $icons_model->init($response); $obj->families[] = $icons_model; } return $obj;
so .. i'm getting:
{ "data": { "families": [ { "id": "1", "name": "a", "image_url": "http://test.co.il/shay/keykit/icons/billadress.png" }, { "id": "2", "name": "b", "image_url": "http://test.co.il/shay/keykit/icons/email.png" }, { "id": "3", "name": "c", "image_url": "http://test.co.il/test/icons/homeicon.png" }, { "id": "6", "name": "f", "image_url": "http://test.co.il/test/icons/homeicon.png" }, { "id": "4", "name": "d", "image_url": "http://test.co.il/test/icons/billadress.png" }, { "id": "5", "name": "e", "image_url": "http://test.co.il/test/icons/billadres2323s.png" },
and on.
how make every 3 object in group? (like first example)
try this:
foreach ($response $key => $response) { $icons_model = new icons_model(); $icons_model->init($response); $obj->families[(int)($key / 3)][] = $icons_model; }
Comments
Post a Comment