Access the data of this Json with PHP

0

[{ "GameId": 53614, "Season": 2018, "SeasonType": 1, "Day": "2018-09-28T00:00:00", "DateTime": "2018-09-28T14:10:00", "Status": "InProgress", "AwayTeamId": 16, "HomeTeamId": 20, "AwayTeamName": "CHW", "HomeTeamName": "MIN", "GlobalGameId": 10053614, "GlobalAwayTeamId": 10000016, "GlobalHomeTeamId": 10000020, "PregameOdds": [{ "GameOddId": 509945, "Sportsbook": "BetDSI", "GameId": 53614, "Created": "2018-09-28T14:12:36", "Updated": "2018-09-28T16:27:12", "HomeMoneyLine": -185, "AwayMoneyLine": 169, "HomePointSpread": -1.7, "AwayPointSpread": 1.7, "HomePointSpreadPayout": 140, "AwayPointSpreadPayout": -163, "OverUnder": 9.0, "OverPayout": -123, "UnderPayout": -123 }], "LiveOdds": [] }]

    
asked by Carlos Maldonado 29.09.2018 в 00:01
source

4 answers

1

I made a small functional code where you can be guided, if you see inside the code place a foreach to go through the array in case if you have more than one index in that array json and the other access directly setting the index if you always want to access only one:

Functional example of how to obtain an array json value

<?php

    $array = '[{
                "GameId": 53614,
                "Season": 2018,
                "SeasonType": 1,
                "Day": "2018-09-28T00:00:00",
                "DateTime": "2018-09-28T14:10:00",
                "Status": "InProgress",
                "AwayTeamId": 16,
                "HomeTeamId": 20,
                "AwayTeamName": "CHW",
                "HomeTeamName": "MIN",
                "GlobalGameId": 10053614,
                "GlobalAwayTeamId": 10000016,
                "GlobalHomeTeamId": 10000020,
                "PregameOdds": [{
                     "GameOddId": 509945,
                     "Sportsbook": "BetDSI",
                     "GameId": 53614,
                     "Created": "2018-09-28T14:12:36",
                     "Updated": "2018-09-28T16:27:12",
                     "HomeMoneyLine": -185,
                     "AwayMoneyLine": 169,
                     "HomePointSpread": -1.7,
                     "AwayPointSpread": 1.7,
                     "HomePointSpreadPayout": 140,
                     "AwayPointSpreadPayout": -163,
                     "OverUnder": 9.0,
                     "OverPayout": -123,
                     "UnderPayout": -123
               }],
               "LiveOdds": []
             }]';

    $array = (array)json_decode($array);

    //Este seria si tuvieses mas de un indice dentro de tu json array
    foreach($array as $clave => $valor){
      echo 'GameId = '.$valor->GameId.'<br>';
      echo 'Season = '.$valor->Season.'<br>';
      echo 'DateTime = '.$valor->DateTime.'<br><br>';

      if(is_array($valor->PregameOdds)){

        foreach($valor->PregameOdds as $clave2 => $valor2){
          echo 'GameOddId = '.$valor2->GameOddId.'<br>';
          echo 'Sportsbook = '.$valor2->Sportsbook.'<br>';
          echo 'Created = '.$valor2->Created.'<br><br>';
        };

      };

    };

    //Este seria si siempre accederias al primer indice de tu array
    echo 'GameId = '.$array[0]->GameId.'<br>';
    echo 'Season = '.$array[0]->Season.'<br>';
    echo 'DateTime = '.$array[0]->DateTime.'<br>';

?>
    
answered by 29.09.2018 в 20:09
0

Check the json_decode function to do the opposite of json_encode

    
answered by 29.09.2018 в 00:05
0

According to the structure, what you have is an array of objects, so you can do it like this:

foreach ($array as $index => $objeto)
{
   echo $objeto['GameId'];

   foreach ($objeto['PregameOdds'] as $i => $obj) {
       echo $obj['GameOddId'];
   }
}
    
answered by 29.09.2018 в 00:07
0

To access this json you can decode it first as an array. If you save that json in a variable called $ json, you could do:

$datos = json_decode($json, true);

Then, to access the data, you have to keep in mind that you have an array of objects. That is, the data is in the first index (0). To access, for example, SeasonType, you have to do:

$seasontype = $datos[0]["SeasonType"];
    
answered by 29.09.2018 в 00:05