donglang5157 2015-04-03 10:12
浏览 153
已采纳

使用PHP简单地创建JSON

PHP: $result = mysql_query("SELECT * FROM events LIMIT 15");

Can anyone help and show me how create json that looks exactly like the one below from the php query above?

{
"events": [
    {
     "id": 2,
        "name": "TIME",
        "image": "http://127.0.0.1/android_tests_json_and_xml/images/time_best.jpg",
        "status": "30 years of Cirque du Soleil's best photos",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/time.png",
        "timeStamp": "1403375851930",
        "url": "http://ti.me/1qW8MLB"
    },
    {
        "id": 3,
        "name": "Discovery",
        "image": "http://127.0.0.1/images/discovery_mos.jpg",
        "status": "A team of Austrian scientists has developed a laser system that causes fruit flies to dance.",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/discovery.jpg",
        "timeStamp": "1403375851930",
        "url": "http://dsc.tv/xmMxD"
    },
    {
        "id": 11,
        "name": "A. R. rahman",
        "image": "http://127.0.0.1/images/ar_bw.jpg",
        "status": "",
        "profilePic": "http://127.0.0.1/android_tests_json_and_xml/images/ar.jpg",
        "timeStamp": "1403375851930",
        "url": ""
    }
]

}

I'd really appreciate it

  • 写回答

3条回答 默认 最新

  • dreinuqm992401 2015-04-03 10:23
    关注

    try like this,

    $rows = array();
    $result = mysql_query("SELECT * FROM events LIMIT 15");
    while($row = mysql_fetch_assoc($result)) {
     $row['events'][] = $row;
    }
    echo  json_encode($rows);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?