'PHP Get value from JSON [duplicate]

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
    }
}

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];

I'm trying to get the first "id" of the achievement.. which would be READY FOR WORK.

How do I fix this?



Solution 1:[1]

When you set the second parameter of json_decode to true, it will return an array.

$json_a=json_decode($string,true);

returns an array.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xdazz