This article demonstrates how to convert a JSON string to an array in PHP.

The standard solution to decode a JSON string is to use the json_decode() function, which takes an JSON encoded string and converts it into an appropriate PHP type. The following code snippet shows how to use json_decode() to convert a JSON string into an object. Note that elements within an object are accessed by encapsulating the element within braces and the apostrophe.

Download  Run Code

 
If you want the json_decode() function to return an associative array instead of an object, you can set its second parameter to true. The second parameter is optional and is false by default; you have to set it to true to convert the JSON objects into associative arrays. For example,

Download  Run Code

 
If your JSON string is encoded using the htmlentities() or htmlspecialchars() function, you might want to decode the HTML entities back to their corresponding characters before invoking json_decode(). This can be done using the html_entity_decode() function, as shown below:

Download  Run Code

That’s all about converting a JSON string to an array in PHP.