Read JSON multidimensional array via PHP

Asked

Viewed 352 times

-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).

  • Ah yes, thank you

2 answers

-1


Dude, you were trying to access a multidimensional, associative array as if it were just $array[0]['Event-home-team']. There are other ways to solve your problem, but the easiest and perhaps consumes less resources that I found was this:

<?php


 $arrays = Array(
'success' => 1,
'result' => Array
    (
        'H2H' => Array
            (
                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,
                    ) )
   ));
    $keys = array_keys($arrays['result']);

for($i = 0; $i < count($keys);$i++){

echo $arrays['result'][$keys[$i]][0]['event_home_team'] . ", " . $arrays['result'][$keys[$i]][0]['league_name'] . "<br>";
    $i++;

}

note that I represented your array like $arrays['result'][$Keys[$i][0]['event_home_team'], I inserted a [0] if it has other items inside that array you have to insert a variable in place and Talvéz a if that adds ++ in that variable.

  • That’s right @Marcosvinicius, it worked was my mistake! Thank you very much!

-1

One way to go through the entire array is:

foreach($json_decode['result'] as $x=>$x_value){
     foreach ($x_value as $y=> $y_value){
    if($y_value['league_name']=! ''){     

        echo $y_value['event_home_team'] . ' x ' ;
        echo $y_value['event_away_team'] . ' - Data do Confronto ' . $y_value['event_date'] . ' - Horário ' . $y_value['event_time'];
        echo '<br>';                   
     }
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.