嗨,我目前正在嘗試執行 API 請求。API 發出這樣的 json 請求:
[
{
"name": "Test 1"
"yes-or-no": "yes"
},
{
"name": "Test 2"
"yes-or-no": "no"
}
]
我的問題是,如何選擇其中之一yes-or-no在網站中回顯?我嘗試這樣做:
<?php
$status = json_decode(file_get_contents('url to the json file'));
// Display message AKA uptime.
foreach ($status->yes-or-no as $answer) {
echo $answer.'<br />';
}
?>
但是沒有用。
如果我弄錯了一些術語,我很抱歉,因為我對這樣的 API 編碼還很陌生。
uj5u.com熱心網友回復:
嘗試這樣的事情:
<?php
$statuses = json_decode(file_get_contents('url to the json file'));
foreach ($statuses as $status) {
echo $status->{'yes-or-no'};
}
?>
uj5u.com熱心網友回復:
我不確定您要做什么,但也許我可以對這個問題有所了解:
$status = json_decode(file_get_contents('url to the json file'), true);
添加 ", true" 這將使您的 $status 成為陣列而不是物件。
foreach ($status as $answer) {
echo $answer['yes-or-no'].'<br />'; //output yes or no
echo $answer['name'].'<br />'; //output test 1 or test 2
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329481.html
上一篇:從動態陣列接收值
