PHP从JSON获取价值

时间:2012-09-26 09:03:43

标签: php json

假设我有这个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:

$string = file_get_contents("achievements.json");
$json_a=json_decode($string,true);

$getit = $json_a->achievement['title'][1];

我正试图获得成就的第一个“id”,即READY FOR WORK

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:9)

当您将json_decode的第二个参数设置为true时,它将返回一个数组。

$json_a=json_decode($string,true);

返回一个数组。

$getit = $json_a['achievement'][1]['title'];