Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been working with parsing some remote JSON with PHP. I have been able to download the JSON and assign it to a variable, and I have used the array functionality with 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.

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.

share|improve this question
    
First search for it than ask it - stackoverflow.com/questions/1028668/… –  Svetlio Apr 6 '13 at 21:58
    
Thanks for the link Svetlio! I missed that one in my earlier searches. –  user1157699 Apr 6 '13 at 22:30

1 Answer 1

up vote 0 down vote accepted
$keys = array_keys($data['files'])
$key = $keys[0]
share|improve this answer
    
Hello, Thank you very much for the quick solution! Works great! –  user1157699 Apr 6 '13 at 22:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.