PHP从JSON获取价值 [英] PHP Get value from JSON
本文介绍了PHP从JSON获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个JSON:
Let's say I have this JSON:
{
"achievement": [
{
"title": "Ready for Work",
"description": "Sign up and get validated",
"xp": 50,
"difficulty": 1,
"level_req": 1
},
{
"title": "All Around Submitter",
"description": "Get one piece of textual content approved in all five areas.",
"xp": 500,
"difficulty": 2,
"level_req": 1
}
}
并且我正在通过PHP进行尝试:
and I am trying this thru PHP:
$string = file_get_contents("achievements.json");
$json_a=json_decode($string,true);
$getit = $json_a->achievement['title'][1];
我正在尝试获取成就的第一个"id",即READY FOR WORK
.
I'm trying to get the first "id" of the achievement.. which would be READY FOR WORK
.
我该如何解决?
推荐答案
将json_decode
的第二个参数设置为true
时,它将返回一个数组.
When you set the second parameter of json_decode
to true
, it will return an array.
$json_a=json_decode($string,true);
返回一个数组.
$getit = $json_a['achievement'][1]['title'];
这篇关于PHP从JSON获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文