json_de code /多维数组访问后JSON数组 [英] Accessing JSON array after json_decode/multidimensional array

查看:111
本文介绍了json_de code /多维数组访问后JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拉着从Twitter的API进行搜索,以获取的file_get_contents的数据,然后传递给json_de code这给我这个阵列结构。

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.

{"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\/",
                        }]

     }]
]

这重复,每鸣叫拉,现在我可以使用foreach循环和结果的每个实例给一个变量赋值,然后从变量提取数据访问的用户名和文本。

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($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']);

这节省了正确的变量确定,但我似乎无法得到的数据结果中实体数组中。如果我打印出实体VAR喜欢的用户名或文字,我得到

This saves the correct variables OK, But I can't seem to get the the data within the entities array within the results. If I print out the Entities var like the username or text I get

ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

所以,它拥有每一个结果返回的数组,但如何在地球上我做访问,我已经有一些其他的方法,我知道访问阵列数据,但他们似乎都属于单位瞎搞。与如何获得这些值,或将它们与集成的foreach将大大AP preciated任何帮助

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

推荐答案

假设您选择去code中的JSON作为一个多维数组,而不是作为对象:

Assuming that you've chosen to decode the JSON as a multi-dimensional array, rather than as objects:

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"];
    }
}

等等

这篇关于json_de code /多维数组访问后JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆