我是JSON的新手,但我試圖弄清楚,但是我得到一個簡單打印輸出的錯誤。如何打印單個JSON元素
我想打印出分數作爲一個簡單的開始。我一直在閱讀關於JSON的內容,但我沒有看到我做錯了什麼。
錯誤消息:
PHP警告:在/testing.php爲的foreach()供給線11
<?php
$string = file_get_contents('URL');
$json = json_decode($string);
foreach ($json['pos'] as $score)
{
echo "pos:". $score['score'] ."\n";
};
print_r($json);
?>
{
"pos": [
{
"pos": "1",
"name": "Sarah",
"game": "a",
"score": "-10",
"examscore": "-5"
},
{
"pos": "T2",
"name": "Brian ",
"game": "F*",
"score": "-8",
"examscore": "-3"
},
{
"pos": "T2",
"name": "Joe",
"game": "F*",
"score": "-8",
"examscore": "-1"
},
{
"pos": "WD"
}
]
}
感謝,但同樣的錯誤 –