我有一个关于使用MySQL查询将我的数据转换为JSON对象的问题。我的查询是转换为JSON对象,
我学会了使用array()并尝试创建一个可以在任何移动应用程序中使用的json。
mysql数据是 -
state_name city_name varient_name price
Uttar Pradesh Lucknow varient1 1202
Uttar Pradesh Lucknow varient2 1203
Uttar Pradesh Lucknow varient3 1204
Delhi Faridabad varient1 1206
Delhi Faridabad varient2 1207
Delhi Faridabad varient3 1208
Delhi Saket varient1 1207
Delhi Saket varient2 1208
Delhi Saket varient3 1209
以及格式所需的响应 -
{
"states": [
{
"state_name": "Uttar Pradesh",
"cities": [
{
"city_name": "Lucknow",
"pricedata": [
{
"varient_name": "varient1",
"varient_price": "1201"
},
{
"varient_name": "varient2",
"varient_price": "1201"
},
{
"varient_name": "varient3",
"varient_price": "1203"
}
]
}
]
},
{
"state_name": "Delhi",
"cities": [
{
"city_name": "Faridabad",
"pricedata": [
{
"varient_name": "varient1",
"varient_price": "1204"
},
{
"varient_name": "varient2",
"varient_price": "1205"
},
{
"varient_name": "varient3",
"varient_price": "1206"
}
]
},
{
"city_name": "Saket",
"pricedata": [
{
"varient_name": "varient1",
"varient_price": "1207"
},
{
"varient_name": "varient2",
"varient_price": "1208"
},
{
"varient_name": "varient3",
"varient_price": "1209"
}
]
}
]
}
]
}
答案 0 :(得分:0)
$aR = array();
$req = mysqli_query("SELECT * FROM table");
while($row = mysqli_fetch_assoc($req))
{
$aR[] = $row;
}
echo '<pre>'.print_r(json_encode($aR),1).'</pre>';