JSON (JavaScript Object Notation) is a popular data format that is widely used in web development. PHP provides several built-in functions to parse JSON data and convert it into PHP data structures.
The json_decode() function can be used to parse a JSON string and return an array. To do this, simply set the second argument of json_decode() to true.
<code class="php">$json = json_decode($data, true);</code>
Once the JSON has been parsed as an array, you can access the data using the array syntax. For example, to access the title of the first product:
<code class="php">echo $json['items'][0]['product']['title'];</code>
Similarly, you can access the brand and description:
<code class="php">echo $json['items'][0]['product']['brand']; echo $json['items'][0]['product']['description'];</code>
To loop through the products and display their information, you can use a foreach loop:
<code class="php">foreach ($json['items'] as $item) {</code>
The above is the detailed content of How to Parse JSON as an Array and Access Data in PHP?. For more information, please follow other related articles on the PHP Chinese website!