0

例如,我在 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 文档很糟糕。

4

2 回答 2

2
$list = json_decode($json_text, true);
foreach ($list as $item) {
    // $item is the each {} set you want
}
于 2013-07-19T01:48:57.017 回答
1

json_decode()是你的朋友吗?将正确的 json 字符串转换为嵌套array()的 php 。

然后,您将foreach遍历结果数组:

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

foreach ($result as $object)
{
    //do stuff
}
于 2013-07-19T01:47:32.540 回答