[英]How to iterate over a JSON object in PHP
For instance, I have an array of these objects returned in a user_timeline from twitter. 例如,我在twitter的user_timeline中返回了这些对象的数组。 The object looks like this: 该对象如下所示:
[{"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": []
}
},
json_decode()
is your friend here. json_decode()
是你的朋友。 Turns proper json strings into nested php array()
s. 将正确的json字符串转换为嵌套的php array()
。
You would then foreach
over the resulting 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.