简体   繁体   中英

php - converting from one json format to another

I am using php to fetch data from mysql database and json encoding the fetched output

json_encode($data)

The resultant data is in the form:

[
    {
        name: "aaa"
    },
    {
        name: "bbb"
    }
]

I want to put this into a bigger json object and output in the following format in the web browser:

{
    content:[
        {
            name: "aaa"
        },
        {
            name: "bbb"
        }
    ]
}

How do i do this using php?

As written in the comment you can just do json_encode(array('content' => $data)) . You can also dynamicly create the array and just feed json_encode with a variable containing the whole array.

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