简体   繁体   中英

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...

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

[
    {
        "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..

hope someone will shed me some light on how to format it by using php

First, get the output of the mysql result in an array using PDO::FETCH_ASSOC

Then convert the array to JSON format using json_encode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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