Home  >  Article  >  Backend Development  >  How to Parse JSON into an Associative Array in PHP?

How to Parse JSON into an Associative Array in PHP?

DDD
DDDOriginal
2024-10-21 13:57:30199browse

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>

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>

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>

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!

Statement:
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