Table of Contents
How do I access data from JSON with PHP?
Home Backend Development PHP Tutorial How Do I Access Data from JSON Using PHP?

How Do I Access Data from JSON Using PHP?

Dec 23, 2024 am 10:09 AM

How Do I Access Data from JSON Using PHP?

How 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"
Copy after login

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"
Copy after login

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"
Copy after login

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"
Copy after login

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;
}
Copy after login

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'
Copy after login

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));
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

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

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

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

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles