-2

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.

Algernop K.
  • 477
  • 2
  • 19

1 Answers1

1

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'];
fusion3k
  • 11,568
  • 4
  • 25
  • 47
  • Okay, but I get "Trying to get property of non-object in" on that particular line even with curly brackets.. – Algernop K. May 03 '16 at 23:54
  • Do you use `True` as second parameter while decoding JSON? – fusion3k May 03 '16 at 23:56
  • I just learned something new. Thank you! I figured myself that the `[0]` with `true` as 2nd parameter for decoding activated would count as another object, act like e.g. `['data']` if you catch my drift. – Algernop K. May 04 '16 at 00:03