php - Why I can't format my JSON object? -
i'm trying construct json object containing inner objects.
i'm trying following code - $ids
array containing ids:
$result = array(); foreach ($ids $value) { $temparray = getcustomoptions($host, $dbusername, $dbpassword, $dbname, $_session['companyid'], $value); array_push($result, $temparray); } print_r(json_encode($result));
the getcustomoptions()
returns array using following script:
$dataarray = []; while ($stmt->fetch()) { $dataarray[] = array( 'id' => $id, 'description' => $description ); }
the problem when print_r(json_encode($result));
i'm getting following result:
[ [ { "id":21, "description":"bshd" }, { "id":22, "description":"gandhi " }, { "id":23, "description":"aaaa" }, { "id":24, "description":"bbbbb" } ], [ { "id":12, "description":"121" }, { "id":13, "description":"qwe" }, { "id":16, "description":"wd2" }, { "id":17, "description":"we" } ], [ ] ]
as can see returns arrays inside of array, need following structure:
{ "data1":[ { "id":21, "description":"bshd" }, { "id":22, "description":"gandhi " }, { "id":23, "description":"aaaa" }, { "id":24, "description":"bbbbb" } ], "data2":[ { "id":12, "description":"121" }, { "id":13, "description":"qwe" }, { "id":16, "description":"wd2" }, { "id":17, "description":"we" } ] }
i know i'm missing small , basic here, me json manipulation in php still hard.
can give me clue or push?
you can try following code generate array in proper format. code not tested please check once.
$result = array(); $i=1; foreach ($ids $value) { $temparray = getcustomoptions($host, $dbusername, $dbpassword, $dbname, $_session['companyid'], $value); $result['data'.$i] = $temparray; $i++; }
Comments
Post a Comment