Skip to main content
se añadieron 2718 caracteres en el cuerpo
Origen Enlace
C47
  • 483
  • 1
  • 6
  • 19

Respuesta editada: Bueno esto es tu json:

$json = '[{
   "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": []
}]';

convertir json a array

$array = json_decode($json,true);

este es el array

array:1 [▼
  0 => array:15 [▼
    "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" => array:1 [▼
      0 => array:14 [▼
        "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" => []
  ]
]

si te puedes dar cuenta, tiene un indice 0, a causa de eso debes hacer esto:

foreach ($array as $k => $v)
{
   echo ($v['GameId']);
   echo ($v['Season']);
   echo ($v['SeasonType']);
   echo ($v['Day']);
   echo ($v['DateTime']);
   //..cuando llegues a PregameOdds este es otro array, entonces debes hacer esto
   echo ($v['PregameOdds'][0]['GameOddId']);
   //...etc   
}

Respuesta editada: Bueno esto es tu json:

$json = '[{
   "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": []
}]';

convertir json a array

$array = json_decode($json,true);

este es el array

array:1 [▼
  0 => array:15 [▼
    "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" => array:1 [▼
      0 => array:14 [▼
        "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" => []
  ]
]

si te puedes dar cuenta, tiene un indice 0, a causa de eso debes hacer esto:

foreach ($array as $k => $v)
{
   echo ($v['GameId']);
   echo ($v['Season']);
   echo ($v['SeasonType']);
   echo ($v['Day']);
   echo ($v['DateTime']);
   //..cuando llegues a PregameOdds este es otro array, entonces debes hacer esto
   echo ($v['PregameOdds'][0]['GameOddId']);
   //...etc   
}
Origen Enlace
C47
  • 483
  • 1
  • 6
  • 19

Aunque ese tu array esta mero raro, de donde lo obtienes? Intenta con esto

$array = 'a tu array';

foreach ($array as $key => $value)
{
   echo $value
}