[英]How to access JSON array with PHP
我正在使用Giantbomb API,它返回的結果是這樣的;
{
error: "OK",
limit: 100,
offset: 0,
number_of_page_results: 24,
number_of_total_results: 24,
status_code: 1,
results: [
{
expected_release_day: 8,
expected_release_month: 5,
name: "Project CARS",
platforms: [
{
api_detail_url: "http://www.giantbomb.com/api/platform/3045-94/",
id: 94,
name: "PC",
site_detail_url: "http://www.giantbomb.com/pc/3045-94/",
abbreviation: "PC"
},
],
site_detail_url: "http://www.giantbomb.com/project-cars/3030-36993/"
},
我可以使用標准的json_decode來訪問最多的信息,然后使用for循環遍歷各項,但是由於某些原因,我在訪問返回的平台數組時遇到了問題。 我正在嘗試獲得這樣的平台名稱:
foreach($games['results'] as $item){
print $item['platforms']['name'];
但是這樣做總是會出現“未定義索引”錯誤。 我在這里做錯了什么?
platforms
內部還有另一個維度,您需要添加另一個索引:
foreach($games['results'] as $item) {
if(isset($item['platforms'][0]['name'])) {
echo $item['platforms'][0]['name'];
}
}
旁注:上面的代碼僅直接指出索引零。 如果該深度內還有更多內容,則在其中添加另一個迭代:
foreach($games['results'] as $item) {
foreach($item['platforms'] as $platform) {
echo $platform['name'];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.