繁体   English   中英

如何从PHP中的json_decode访问嵌套数组?

[英]How do I access nested arrays from a json_decode in PHP?

我花了几个小时在这里,我已经在stackoverflow上阅读了大量的答案,没有人帮助过。

到目前为止,我所能做的就是打印Market Square - Clifden这个例子。

$obj = json_decode($data);
$obj = $obj[0];
print $obj->{'title'};

我无法弄清楚如何访问嵌套在“图像”中的“名称”,所以我可以获得market_square_clifden.jpg

我会感激一些指示。

array(1){
        [0]=>array(12){
            ["_id"]=>array(1){
                ["$oid"]=>string(24)"51f674e4e4b0066cc8197033"
            }
            ["display"]=>int(1)
            ["title"]=>string(23)"Market Square - Clifden"
            ["class"]=>string(21)"market_square_clifden"
            ["icon"]=>string(16)"camera_small.png"
            ["image"]=>array(4){
                ["name"]=>string(25)"market_square_clifden.jpg"
                ["top"]=>string(16)"7.98958587646484"
                ["left"]=>string(18)"397.98614501953125"
                ["maxwidth"]=>string(16)"599.777777671814"
            }
            ["locs"]=>array(2){
                ["lng"]=>float(-10.022516)
                ["lat"]=>float(53.488111)
            }
            ["pov"]=>array(3){
                ["heading"]=>string(17)"-14.1950626239811"
                ["pitch"]=>string(18)"-6.368221166504443"
                ["zoom"]=>string(18)"0.8399999999999999"
            }
            ["photo"]=>array(3){
                ["takenby"]=>string(13)"Robert French"
                ["sentinby"]=>string(34)"The Lawrence Photograph Collection"
                ["description"]=>string(263)"Clifden (Irish: An Clochán, meaning 'stepping stones' is a town on the coast of County Galway, Ireland and being Connemara's largest town, it is often referred to as 'the Capital of Connemara'. It is located on the Owenglen River where it flows into Clifden Bay."
            }
            ["date"]=>array(2){
                ["posted"]=>string(53)"Mon Jul 29 2013 14:53:53 GMT+0100 (GMT Daylight Time)"
                ["circa"]=>string(9)"1880-1900"
            }
            ["comments"]=>array(1){
                [0]=>array(2){
                    ["poster"]=>string(0)""
                    ["comment"]=>string(0)""
                }
            }
            ["tags"]=>array(1){
                [0]=>array(2){
                    ["name"]=>string(0)""
                    ["slug"]=>string(0)""
                }
            }
        }
    }

正如评论者提到的那样,请修改您的变量格式 - 它有助于更​​快地获得答案。

假设您使用默认的json_decode设置,每个JSON对象将成为PHP对象。 (参见php.net上的json_decode文档 。)

$obj = json_decode($data);
$obj = $obj[0];
// title attribute
$obj->title
// image object
$obj->image
// image name
$obj->image->name

如果强制所有内容都是关联数组:

$obj = json_decode($data,true);
$obj = $obj[0];
// title attribute
$obj['title']
// image object
$obj['image']
// image name
$obj['image']['name']

您可以使用以下代码获取它。 您可以将所有数组视为php中的字典或关联数组。 它们由数字(常规数组)索引,或者由键(关联数组)索引。

$obj[0]["image"]["name"]

在嵌套数组的实例中,当您访问内部数组时,可以将其视为数组。 可以这样想:

$innerArray = $obj[0];
// The image key gives us an array as well.
$image = $innerArray["image"];
// Now we have the dictionary where the name is
$name = $image["name"];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM