-2
stdClass Object ( 
    [api] => stdClass Object ( 
        [results] => 1 
        [leagues] => stdClass Object ( 
            [1] => stdClass Object ( 
                [league_id] => 1 
                [name] => World Cup 
                [country] => World 
                [country_code] => 
                [season] => 2018 
                [season_start] => 2018-06-14 
                [season_end] => 2018-07-15 
                [logo] => https://media.api-football.com/leagues/1.png 
                [flag] => 
                [standings] => 1 
            ) 
        ) 
    ) 
)

$data= json_decode($response,false);

I must read the element [league_id]

I tried to write

echo $data->api->leagues[1]->league_id;

seeing also the previous examples, but nothing is printed, I can't understand what I'm wrong. Could someone help me, thanks? thanks

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

1

leagues is an object, not an array, so you need to use -> to access the property. And since it's a number rather than an identifier, you have to wrap the property name in braces.

echo $data->api->leagues->{"1"}->league_id;
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

If you change the second parameter from json_decode to true, maybe you will access in this way:

echo $data['api']['leagues']['1']['league_id'];