Home > Article > Backend Development > How to Parse JSON into an Associative Array in 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!