How do I convert JSON to PHP array?

To convert a JSON data string to a PHP array, you can use the json_decode() function. The json_decode() function accepts the JSON string as the first parameter and a few additional parameters to control the process of converting JSON to PHP array. If the JSON cannot be converted or the data nesting depth exceeds the recursion limit, the converting function will return NULL. In this JSON convert to a PHP array example, we use the json_decode() function to convert JSON string to a PHP array. Click Execute to run the JSON to PHP Array Example online and see the result.
How do I convert JSON to PHP array? Execute
<?php
$json = '{"UK": "London", "ES": "Madrid", "IT": "Rome"}';

print_r (json_decode($json));
?>
Updated: Viewed: 1603 times

What is JSON?

JSON (JavaScript Object Notation) is a lightweight text-based, language-independentf data exchange format. Despite its origins in JavaScript, this format is considered language-independent; JSON can use with almost any programming language, including JavaScript, Java, C ++, C #, Go, PHP, Python, and many others.

How to decode JSON string into PHP object?

You can use the json_decode() function to convert JSON strings into PHP objects. The result object can be any PHP data type, including an array, except for resource pointers such as a database or file descriptor.

JSON to PHP Array Converter Example
<?php
$json = '{"UK": "London", "ES": "Madrid", "IT": "Rome"}';

print_r (json_decode($json));
?>

#output: stdClass Object
(
    [UK] => London
    [ES] => Madrid
    [IT] => Rome
)

See also