doumao8803 2017-09-29 19:35
浏览 28

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

I have a question about using a MySQL Query to convert my data into a JSON Object. The Query I have is converting to a JSON Object,

I have learned using array() and trying to create a json which I can use in any mobile application.

mysql data is -

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"
                        }
                    ]
                }
            ]
        }
    ]
}
  • 写回答

1条回答 默认 最新

  • douke1954 2017-09-29 19:57
    关注
    $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>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭