I have create the json file from the sql table but the output is not same with the output i expected. Below is my code to create the json file from the sql table
<?php
include 'config.php';
$sql = "SELECT table_id, table_name,GROUP_CONCAT(price) price FROM tables INNER JOIN table_orders ON table_id = res_table_id GROUP BY table_id";
$result = mysqli_query($mysqli,$sql);
while($data1 = $result->fetch_assoc()){
$data[] = $data1;
}
$encoded_data = json_encode($data,JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
file_put_contents('data_json',$encoded_data);
?>
The output will look like this
[
{
"table_id": "1",
"table_name": "Table 1",
"price": "20.00,15.00,7.90"
},
{
"table_id": "2",
"table_name": "Table 2",
"price": "7.90,15.00"
}
]
I want my result something like the output below:
{"table":[
{
"table_id": "1",
"table_name": "Table 1",
"price": [
"20.00","15.00","7.90"
]
},
{
"table_id": "2",
"table_name": "Table 2",
"price": [
"7.90","15.00"
]
}
]}
2
Answers
What this does:
In your initial loop, you first format your array items in the way you want. Then you can format your JSON string in the nested structure you want by passing in the format you like.
There are a few ways to optimize this, but that’s the gist of it.