简体   繁体   English

从mysql输出正确的复杂json格式

[英]outputting proper complex json format from mysql

I have issues when I attempt to outputting record from mysql for complex json to send back to jquery... 当我尝试从mysql输出记录以将复杂的json发送回jquery时遇到问题...

my table 我的桌子

bil    iduser     name      experience
0       001       jacobs    bus driver 
1       002       max       painter
2       001       jacobs    racing driver
3       003       john      engineer
4       001       jacobs    retiree
5       002       max       designer
6       003       john      senior engineer

the desire json result is should be 愿望json结果应该是

[
    {
        "iduser":001,
        "name":"jacobs", 
        "exprience":[{"exp":"bus driver"},{"exp":"racing driver"},{"exp":"retiree"}]
    },
    {
        "iduser":002,
        "name":"max", 
        "exprience":[{"exp":"painter"},{"exp":"designer"}]
    }
]

and sort of....I'm okay with simple json format but this type of complex format I'm totally failed...stuck up here.. 等等....我可以使用简单的json格式,但是这种复杂的格式我完全失败了...卡在这里..

hope someone will shed me some light on how to format it by using php 希望有人能使我了解如何使用php格式化

First, get the output of the mysql result in an array using PDO::FETCH_ASSOC 首先,使用PDO :: FETCH_ASSOC在数组中获取 mysql结果的输出

Then convert the array to JSON format using json_encode 然后使用json_encode将数组转换为JSON格式

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

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