简体   繁体   English

获取PHP数组中嵌套键名的值

[英]Get value of nested key name in php array

I have been working with parsing some remote JSON with PHP. 我一直在用PHP解析一些远程JSON。 I have been able to download the JSON and assign it to a variable, and I have used the array functionality with json_decode: 我已经能够下载JSON并将其分配给变量,并且已经将数组功能与json_decode结合使用:

$data = json_decode($remotejson, true);

I have then printed the complete array back to verify the contents of the array: 然后,我将完整的数组打印回去以验证数组的内容:

echo print_r($data);

The array prints back and I can see the keys and values: 数组打印回来,我可以看到键和值:

[files] => Array
    (
        [/photogalleryupload.thumbs/1934307_000001.jpg] => Array
            (
                [source] => derivative
                [format] => Thumbnail
                [original] => moviefile_1934307.mp4
            )

I am trying to get the value of the first nested key name which is "/photogalleryupload.thumbs/1934307_000001.jpg" and assign it to a variable. 我正在尝试获取第一个嵌套键名的值,即“ /photogalleryupload.thumbs/1934307_000001.jpg”,并将其分配给变量。

For example, I would like the following code: 例如,我想要以下代码:

echo $data['files'][0];

To return this: 返回此:

/photogalleryupload.thumbs/1934307_000001.jpg

This does not work. 这是行不通的。

The difficulty I am having is that this value I am trying to return is a 2nd level key name and I have been having trouble finding a way of assigning it to a variable. 我遇到的困难是,我要返回的该值是第二级键名,而我一直难以找到将其分配给变量的方法。

$keys = array_keys($data['files'])
$key = $keys[0]

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

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