How Do I Access Data from JSON Using PHP?
Dec 23, 2024 am 10:09 AMHow do I access data from JSON with PHP?
PHP provides the json_decode() function to decode a JSON string and convert it into a PHP data structure. Let’s dig into how to access the results:
Object property access:
Object properties can be accessed via $object->property For example:
$json = '{"type": "donut", "name": "Cake"}'; $yummy = json_decode($json); echo $yummy->type; // "donut"
Array element access:
Array elements can be accessed through $array[0] For example:
$json = '["Glazed", "Chocolate with Sprinkles", "Maple"]'; $toppings = json_decode($json); echo $toppings[1]; // "Chocolate with Sprinkles"
Nested item access:
Nested items can be accessed through consecutive attributes and indexes, such as $object-> array[0]->etc. :
$json = '{"type": "donut", "name": "Cake", "toppings": [{"id": "5002", "type": "Glazed"}]}'; $yummy = json_decode($json); echo $yummy->toppings[0]->id; // "5002"
Convert to associative array:
Pass true as the second parameter of json_decode() to convert JSON The object is decoded into an associative array whose keys are strings:
$json = '{"type": "donut", "name": "Cake"}'; $yummy = json_decode($json, true); echo $yummy['type']; // "donut"
Accessing the associative array items:
can be done via foreach (array_expression as $key => $ value) Traverse keys and values:
$json = '{"foo": "foo value", "bar": "bar value", "baz": "baz value"}'; $assoc = json_decode($json, true); foreach ($assoc as $key => $value) { echo "The value of key '$key' is '$value'" . PHP_EOL; }
Output:
The value of key 'foo' is 'foo value' The value of key 'bar' is 'bar value' The value of key 'baz' is 'baz value'
Unknown data structure:
If you don’t know the data structure, please refer to the related documentation or use print_r() Check the result:
print_r(json_decode($json));
json_decode() returns null:
This happens when the JSON is null or invalid. Use json_last_error_msg() to check error messages.
Special character attribute names:
Use {"@attributes":{"answer":42}} to access attribute names with special characters:
$json = '{"@attributes":{"answer":42}}'; $thing = json_decode($json); echo $thing->{'@attributes'}->answer; //42
The above is the detailed content of How Do I Access Data from JSON Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey
