-3
Gentlemen, I’m having difficulty reading a JSON that comes via API, I receive it in the following way:
$json_file = file_get_contents("https://allsportsapi.com/api/football/?met=H2H&APIkey=MINHA_KEY");
$json_decode = json_decode($json_file, true);
echo "<pre>";
print_r($json_decode);
Through a print_r I have the following return:
Array(
[success] => 1
[result] => Array
(
[H2H] => Array
(
[0] => Array
(
[event_key] => 273338
[event_date] => 2020-01-21
[event_time] => 21:15
[event_home_team] => Chelsea
[home_team_key] => 2616
[event_away_team] => Arsenal
[away_team_key] => 2617
[event_halftime_result] => 1 - 0
[event_final_result] => 2 - 2
[event_status] => Finished
[country_name] => England
[league_name] => Premier League
[league_key] => 148
[league_round] => Round 24
[league_season] =>
[event_live] => 0
[event_country_key] => 41
) )
[firstTeamResults] => Array
(
[0] => Array
(
[event_key] => 273383
[event_date] => 2020-03-08
[event_time] => 15:00
[event_home_team] => Chelsea
[home_team_key] => 2616
[event_away_team] => Everton
[away_team_key] => 2612
[event_halftime_result] => 2 - 0
[event_final_result] => 4 - 0
[event_status] => Finished
[country_name] => England
[league_name] => Premier League
[league_key] => 148
[league_round] => Round 29
[league_season] =>
[event_live] => 0
[event_country_key] => 41
) )
[secondTeamResults] => Array
(
[0] => Array
(
[event_key] => 273381
[event_date] => 2020-03-07
[event_time] => 16:00
[event_home_team] => Arsenal
[home_team_key] => 2617
[event_away_team] => West Ham
[away_team_key] => 2620
[event_halftime_result] => 0 - 0
[event_final_result] => 1 - 0
[event_status] => Finished
[country_name] => England
[league_name] => Premier League
[league_key] => 148
[league_round] => Round 29
[league_season] =>
[event_live] => 0
[event_country_key] => 41
) ) )
Where to read I use a foreach:
if(count($json_decode)){
$i = 0;
foreach ($json_decode as $value) {
echo $value["event_home_team"] . ", " . $value["league_name"] . "<br>";
} } else {
echo "nenhum jogo no momento"; }
Where I’m missing the return of values?
Return me the following message: Trying to get Property of non-object
Thank you
No need to put "Solved" in the title. I know it is common practice in many forums, but here it is different. You already accepted the reply below (clicked on ) and this is enough to indicate that the problem has been solved. If you want to put your own solution, use the answer field below (see the text field "Your answer" at the bottom of the page).
– hkotsubo
Ah yes, thank you
– Cristiano Facirolli