簡體   English   中英

如何在PHP中迭代JSON對象

[英]How to iterate over a JSON object in PHP

例如,我在twitter的user_timeline中返回了這些對象的數組。 該對象如下所示:

[{"created_at": "Fri Nov 02 23:44:11 +0000 2012", "id": 264513266083049472, "id_str": "264513266083049472", "text": "@JessLeighMusic do it! This time my dad will be playing!", "source": "\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e", "truncated": false, "in_reply_to_status_id": 264489640013217793, "in_reply_to_status_id_str": "264489640013217793", "in_reply_to_user_id": 38814642, "in_reply_to_user_id_str": "38814642", "in_reply_to_screen_name": "JessLeighMusic", "user": {
"id": 143161594,
"id_str": "143161594",
"name": "Mandala Faulkner",
"screen_name": "Mandalastar",
"location": "Ada, Ok",
"description": "I love to sing, play guitar, piano, and flute, but I am still learning everyday. I perform at the Quality Inn the first and third Friday of every month.",
"url": null,
"entities": {
    "description": {
        "urls": []
    }
},

等等等等。 這是我的問題:在PHP中使用foreach時,如何指示代碼“向下鑽取”到每個{}集合中的元素? 我之前從未使用過JSON對象,Twitter的API文檔非常糟糕。

json_decode()是你的朋友。 將正確的json字符串轉換為嵌套的php array()

這樣,你會foreach在結果數組:

$json = "json string here";
$result = json_decode($json);

foreach ($result as $object)
{
    //do stuff
}
$list = json_decode($json_text, true);
foreach ($list as $item) {
    // $item is the each {} set you want
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM