PHP and JSON
JSON stands for JavaScript Object Notation, and is a syntax for storing and exchanging data.
PHP and JSON
PHP has some built-in functions to handle JSON.
The following two functions allows to encode and decode JSON.
- json_encode()
- json_decode()
PHP - json_encode()
The json_encode() function is used to encode a value to JSON format.
Example
This Example shows how to encode an associative array into a JSON object:
PHP - json_decode()
The json_decode() function is used to decode a JSON object into a PHP object.
Example
This Example decodes JSON data into a PHP object.
PHP - Accessing the Decoded Values
The following two Examples which explains how to access the decoded values from an object and from an associative array:
Example
This Example shows how to access the values from a PHP object:
Example
This Example shows how to access the values from a PHP associative array:
PHP - Looping Through the Values
Loop through the values by using a foreach() loop:
Example
This Example shows how to loop through the values of a PHP object:
Example
This Example shows how to loop through the values of a PHP associative array: