Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?php
include '../dbconfig.php';
$currentbooking="Some Mysqli Query";

$result = mysqli_query($link, $currentbooking);
$response = array();
while($row = mysqli_fetch_assoc($result))
{
$response[]=$row;
}
echo json_encode($response);
?>
I am getting response as below
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.

[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},

]

What I have tried:

Plz I don't have any idea how to get the specified JSON array.Plz help me with how to go with it.I want the specified JSON format in PHP.
I am using Core PHP
Posted
Updated 10-Mar-18 4:58am
v2
Comments
Richard MacCutchan 10-Mar-18 8:06am    
Where is this data coming from?
Rohan Sawant 10-Mar-18 8:31am    
Data is coming from DB
Richard MacCutchan 10-Mar-18 9:03am    
Please edit your question and provide proper details of what you are doing, what you are trying to do, and what programming problem you have.
Rohan Sawant 10-Mar-18 10:59am    
I am using PHP.I am not able to convert required array from what i got as response.
Ali Al Omairi(Abu AlHassan) 11-Mar-18 8:22am    
I think you need to group your data before you call json_encode() as you want more than a flat array

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900