Which functions are used to encode and decode JSON file in PHP ?
Parsing JSON data in PHP: There are built-in functions in PHP for both encoding and decoding JSON data. These functions are json_encode() and json_decode(). These functions works only with UTF-8 encoded string.
Decoding JSON data in PHP: It is very easy to decode JSON data in PHP. You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type.
Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.
Encoding JSON data in PHP: Encoding JSON data is as easy as encoding JSON data in PHP. We use the json_encode() function, the data being encoded can be any PHP data type except a resource like a filehandle.
Example 1: The following code demonstrates encoding PHP associative array.
Example 2: The following code demonstrates encoding PHP indexed array.