0

My code:

$json_response = json_decode($response, true);
$tag= $json_response['results']['tags'][0]['tag'];
print $tag;

My JSON:

{
  "results": [
    {
      "tagging_id": null,
      "image": "image.jpg",
      "tags": [
        {
          "confidence": 100,
          "tag": "herb"
        },
        {
          "confidence": 98.3637619018555,
          "tag": "plant"
        }
      ]
    }
  ]
}

I am trying to output "herb". I have looked up through examples but cannot figure out where the bug is.

I am reading results and then the trees.

5
  • 1
    Do a print_r($json_response); to see the nesting. $tag = $json_response['results'][0]['tags'][0]['tag']; Commented Jun 19, 2018 at 18:15
  • pastebin.com/5b3ysyDu Commented Jun 19, 2018 at 18:20
  • 1
    3v4l.org/kki58 Commented Jun 19, 2018 at 18:23
  • Position results is an array of objects; in this case there is only one object in the array. Commented Jun 19, 2018 at 18:23
  • Heh... this issue is the exact opposite of a prior issue you had: stackoverflow.com/questions/41488760/… Thats kinda funny ;) Commented Jun 19, 2018 at 18:41

1 Answer 1

3

You have to fetch the first element inside ['results'].

echo $json_response['results'][0]['tags'][0]['tag'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.