[英]Accessing JSON array after json_decode/multidimensional array
I'm pulling in a search from the Twitter api, fetching the data with file_get_contents and then passing to json_decode which give me this array structure. 我正在从Twitter api进行搜索,使用file_get_contents获取数据,然后传递给json_decode,这给了我这个数组结构。
{"results":[
{
"from_user":"A-user-name",
"from_user_id":457304735,
"text":"Ich R U #BoysNoize #SuperRola",
"entities":{
"urls":[{
"url":"http:\/\/t.co\/WZnUf68j",
"expanded_url":"http:\/\/instagr.am\/p\/Vz4Nnbnjd6\/",
}]
}]
]
This repeats for every tweet pulled, Now I can access the user name and text using a foreach loop and assigning every instance of results to a variable, then pulling data from the variable. 对于每个推送的推文重复这一步骤。现在,我可以使用foreach循环访问用户名和文本,并将每个结果实例分配给变量,然后从变量中提取数据。
foreach($jsonArr['results'] as $item){
// Takes the Array jsonArr and for every results heading creates an $item
$user = mysql_real_escape_string($item['from_user']);
$text = mysql_real_escape_string($item['text']);
This saves the correct variables OK, But I can't seem to get the the data within the entities array within the results. 这样可以保存正确的变量OK,但我似乎无法在结果中获取实体数组中的数据。 If I print out the Entities var like the username or text I get 如果我打印出实体var,就像我得到的用户名或文字一样
ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray
So It holds the arrays for each results returned but how on earth do I access it, I've been messing around with a few other methods I know for accessing array data but they all seem to fall flat. 所以它保留了返回的每个结果的数组,但我到底如何访问它,我一直在搞乱我知道访问数组数据的其他一些方法,但它们似乎都没有。 Any help with how to get at these values, or integrate them with the foreach would be greatly appreciated 任何有关如何获取这些值或将它们与foreach集成的帮助将不胜感激
Assuming that you've chosen to decode the JSON as a multi-dimensional array, rather than as objects: 假设您已选择将JSON解码为多维数组,而不是作为对象:
foreach ($results as $tweet) {
$user = $tweet["from-user"];
$text = $tweet["text"];
$entities = $tweet["enities"];
$urls = $entities["urls"];
foreach ($urls as $url) {
echo $url["expanded_url"];
}
}
et cetera 等等
Array
(
[results] => Array
(
[0] => stdClass Object
(
[entities] => Array
(
[0] => stdClass Object
(
[urls] => Array
(
[0] => stdClass Object
(
[expanded_url] => http://instagr.am/p/Vz4Nnbnjd6/
[url] => http://t.co/WZnUf68j
)
)
)
)
[from_user] => A-user-name
[from_user_id] => 457304735
[text] => Ich R U #BoysNoize #SuperRola
)
)
)
Accessing url: 访问网址:
$json_array['results'][0]->entities[0]->urls[0]->url;
Helpful code: 有用的代码:
<?php
$json ='{ "results" : [ { "entities" :
[ { "urls" : [ { "expanded_url" : "http://instagr.am/p/Vz4Nnbnjd6/",
"url" : "http://t.co/WZnUf68j"
} ] } ],
"from_user" : "A-user-name",
"from_user_id" : 457304735,
"text" : "Ich R U #BoysNoize #SuperRola"
} ] }';
$json_array = (array)(json_decode($json));
echo '<pre>';
//print_r($json_array);
echo $json_array['results'][0]->entities[0]->urls[0]->url;
?> ?>
Just do a print_r($jsonArr);
做一个print_r($jsonArr);
and you will be able to work with your decoded json. 并且您将能够使用已解码的json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.