假设我的JSON是这样的:
{
"achievement": [
{
"title": "All Around Submitter",
"description": "Get one piece of content approved in all six areas.",
"xp": 500,
"level_req": 1
},
{
"title": "World-wide Photo Journalist",
"description": "Get one photo approved in all six areas.",
"xp": 500,
"level_req": 1
},
{
"title": "Ready for Work",
"description": "Sign up and get validated",
"xp": 50,
"level_req": 1
},
{
"title": "Asian Pride",
"description": "Get ten pieces of content approved to a club in an Asian nation.",
"xp": 1500,
"level_req": 1
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的PHP代码是这样的,所以我加载了json文件...:
<?php
$string = file_get_contents("achievements.json");
$json_a=json_decode($string,true);
foreach ($json_a as $key => $value){
echo $value[0]['title'] . " <br />";
}
?>
Run Code Online (Sandbox Code Playgroud)
但是它只输出第一个数组.我知道.只有1.但foreach循环怎么样?为什么不是每行循环?
您正在遍历顶级数组,该数组只有一个键: achievement
相反,你应该
foreach ($json_a['achievement'] as $key => $value){
echo $value['title'] . " <br />";
}
Run Code Online (Sandbox Code Playgroud)
提示:[0]是一种代码味道.
| 归档时间: |
|
| 查看次数: |
423 次 |
| 最近记录: |