douyu4535 2016-02-01 08:19
浏览 47
已采纳

php - 从一种json格式转换为另一种格式

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?

  • 写回答

1条回答 默认 最新

  • dongliang2005 2016-02-01 08:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?