How to Parse JSON into an Associative Array in PHP?

DDD
Release: 2024-10-21 13:57:30
Original
124 people have browsed it

How to Parse JSON into an Associative Array in PHP?

Parsing JSON with PHP

When working with APIs like Google's Shopping API, you may encounter JSON data. PHP provides the json_decode() function to parse JSON into a PHP variable.

<code class="php">$json = json_decode($data);</code>
Copy after login

However, this typically results in an object hierarchy, making it difficult to access nested data. To parse JSON as an array, use:

<code class="php">$json = json_decode($data, true);</code>
Copy after login

This returns an associative array, simplifying data access. For example, to retrieve the title, brand, and description of each product:

<code class="php">foreach ($json['items'] as $item) {</code>
Copy after login

The above is the detailed content of How to Parse JSON into an Associative Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!