简体   繁体   English

如何使用php将mysql数据转换为所需的json格式

[英]How to convert mysql data into desired json format using php

I have a question about using a MySQL Query to convert my data into a JSON Object.我有一个关于使用 MySQL 查询将我的数据转换为 JSON 对象的问题。 The Query I have is converting to a JSON Object,我的查询正在转换为 JSON 对象,

I have learned using array() and trying to create a json which I can use in any mobile application.我已经学会了使用 array() 并尝试创建一个可以在任何移动应用程序中使用的 json。

mysql data is - 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

and the response needed in the format -以及格式所需的响应 -

{
    "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"
                        }
                    ]
                }
            ]
        }
    ]
}
$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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM