John Smith John Smith - 7 months ago 25
PHP Question

Targeting object within array JSON Steam API

How do I access

webm->max
in this Steam API? It's the order
[{
that confuses me, array of one before object? I'm not quite sure about the targeting here..

I've tried:

$gameTrailer = $game_json->57690->data->movies[0]->webm->max;


and

$gameTrailer = $game_json['57690']['data']['movies']['webm']['max'];


The API text is like this:

"movies": [{
"id": 2029441,
"name": "Tropico 4 Gameplay Trailer",
"thumbnail": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2029441\/movie.293x165.jpg?t=1447358847",
"webm": {
"480": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2029441\/movie480.webm?t=1447358847",
"max": "http:\/\/cdn.akamai.steamstatic.com\/steam\/apps\/2029441\/movie_max.webm?t=1447358847"
},
"highlight": true
}],


and "movies" lies within:

{"57690": {
"data": {


Assume I'll always want the very first movie in an array (which in this case is an array of one). Thanks in advance.

Answer

Correct syntax:

$game_json->{57690}->data->movies[0]->webm->max

When you have an object with a numeric key, you have to wrap the key name by curly brackets (Numeric keys are not valid property names).

If you use the associative option:

json_decode( $data, True );

your second attempt is almost right. Simply add the correct index after movie:

$gameTrailer = $game_json['57690']['data']['movies'][0]['webm']['max'];