I really need your help.
I know how to create with PHP and SQL a json array.
I am creating mine
But I need to create a nested one, and I kinda got lost there.
I saw many other questions similar to this one but still cant get it.
I want my array to looks like this one:
"names": {
"name_id": {
"firstname": "john",
"lastname": "doe",
"age": 18,
"menu": {
"dinner": {
"dinner1": "119",
"dinner2": "229",
"dinner3": "379",
"dinner4": "559"
}
},
"deep_link_url": "http:\/\/testurl.com\/?name_id=42422&ref=CC",
"bonus_offers": "some_bonus"
},
Here is my code:
$return_arr = array();
$sql = "sp_getdata";
$fetch = sqlsrv_query($connection,$sql);
while ($row = sqlsrv_fetch_array($fetch)) {
$row_array['name_id'] = $row['name_id'];
$row_array['firstname'] = $row['firstname'];
$row_array['lastname'] = $row['lastname'];
$row_array['age'] = $row['age'];
$row_array['dinner1'] = $row['dinner1'];
$row_array['dinner2'] = $row['dinner2'];
$row_array['dinner3'] = $row['dinner3'];
$row_array['dinner4'] = $row['dinner14'];
$row_array['deep_link_url'] = url().'/name_id='.$name_id;
$row_array['bonus_offers'] = $row['bonus_offer'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
I get everything into just one. Whats the way to make them nested?
Sorry if my question is not clear enough for someone but that's how I am imagine that in my head. Please someone explain and help!
$row_arrayshould be multidimensional array