猿问

如何使用php foreach读取json数组?

我想在 JSON 数据中获取游戏的 id、competitionId、competitionDisplayName。我当前的代码没有输出任何内容,谁能告诉我我做错了什么。谢谢


$json_string = 'https://webws.365scores.com/web/games/?langId=27&timezoneName=Asia/Hebron&userCountryId=-1&appTypeId=5&sports=1&startDate=17/08/2019&endDate=17/08/2019';

$json = file_get_contents($json_string);

$array = json_decode($json);


foreach($array as $values)

{

    $output = $values->games['competitionDisplayName'] . "\n";

}


echo $output;


慕桂英4014372
浏览 702回答 2
2回答

偶然的你

我检查了您的网址,游戏的数据是一个数组,因此您需要遍历它以获取每个游戏的值。$array = json_decode($json);foreach($array->games as $game){    $output = $game->competitionDisplayName;    prinr_r($output);}

手掌心

尝试这个:<?php$json_string = 'https://webws.365scores.com/web/games/?langId=27&timezoneName=Asia/Hebron&userCountryId=-1&appTypeId=5&sports=1&startDate=17/08/2019&endDate=17/08/2019';$json = file_get_contents($json_string);$result = json_decode($json);foreach ($result->games as $game) {&nbsp; &nbsp;$output .= $game->competitionDisplayName . "\n";}echo $output;
随时随地看视频慕课网APP
我要回答