search

Home  >  Q&A  >  body text

Extracting and accessing JSON data using PHP: a step-by-step guide

This is a general reference question and answer covering many of the never-ending "How do I access data in JSON?" questions. It deals here with the broad basics of decoding JSON in PHP and accessing the results.

I have JSON:

{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}

How to decode it and access the resulting data in PHP?

P粉231112437P粉231112437428 days ago539

reply all(1)I'll reply

  • P粉162773626

    P粉1627736262023-10-13 11:40:14

    In this example, json_decode() is used to decode JSON data into a PHP associative array. You can then access the individual elements of the array just like any PHP array.

    reply
    0
  • Cancelreply