在foreach循环中获取json值

时间:2018-09-17 10:07:33

标签: php foreach

我的链接json enter link description here

获取它stage_name,start_time,name

$someObject = file_get_contents("http://varzesht.com/my/live.php");
foreach($someObject as $value) {
    echo $value->stage_name;
    foreach($someObject->matches as $value) {
        echo $value->start_time . "<br>". $value->name;
    }
}

1 个答案:

答案 0 :(得分:2)

1。您需要先使用json_decode()

解码json。

2。您需要在第二个$value中使用$someObject->matches而不是foreach()

$someObject = file_get_contents("http://varzesht.com/my/live.php");
$array_data = json_decode($someObject); //decode json data and assign it to a new variable
foreach($array_data as $value) { // use that new variable to iterate
    echo $value->stage_name;
    foreach($value->matches as $val) { // use $value here
        echo $val->home_team->start_time . "<br>". $val->home_team->name;
    }
}