Converting JSON to PHP Array

To convert a JSON data string to a PHP array, you can use the json_decode($json) 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 a PHP array. If the JSON cannot be converted to an array or the data nesting depth exceeds the recursion limit, the converting function will return NULL. In this JSON Convert to 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.
Converting JSON to PHP Array Execute
<?php
   $json = '{"UK": "London", "ES": "Madrid", "IT": "Rome"}';
   print_r (json_decode($json));
?>
Updated: Viewed: 16519 times

What is PHP?

PHP is a versatile, open-source server-side scripting language used for web development and can be embedded in HTML code. PHP is known for its ease of use, flexibility, and a large community of developers. PHP is compatible with many web servers and databases. PHP supports multiple databases such as MySQL, PostgreSQL, and SQLite and can run on many operating systems, including Windows, Linux, and macOS.

What is an array in PHP?

An array in PHP is a variable that we use to store a data set containing various elements. The elements of an array are identified by a unique key or index, which can be a number or a string. PHP has two types of arrays: indexed arrays and associative arrays. PHP provides many built-in functions for manipulating arrays, including sorting, searching, checking if an element exists in an array, splitting string to array, converting array to string, converting array to JSON, and getting the length of an array.

What is JSON?

JSON (JavaScript Object Notation) is a language-independent text format for storing and exchanging data. JSON is widely used for web APIs, storing configuration data, exchanging data between web applications, and transmitting data between client and server in web applications. Many programming languages have built-in or external libraries for creating and manipulating JSON data strings, including JavaScript, Python, C++, C#, Go, PHP, and Java.

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
)

Converting JSON to PHP Array Examples

The following are examples of converting JSON to an array in PHP:

Converting JSON Array to Array

A JSON array in PHP is a data structure that allows you to store an ordered set of values in JSON format. The following is an example of converting a JSON array to a PHP array using the json_decode() function:

Converting JSON Array to Array Example
<?php
$json = '["JSON", "to", "PHP", "Array"]';

print_r (json_decode($json));
?>

#output: Array
(
    [0] => JSON
    [1] => to
    [2] => PHP
    [3] => Array
)

Converting Nested JSON string to Array

A nested JSON string in PHP is a JSON string that includes other objects or arrays. In PHP, such an object can be created using a multidimensional array. The following is an example of nesting a JSON string into a PHP array using the json_decode() function:

Converting Nested JSON string to Array Example
<?php
$json = '{"name": "Leo", "age": 28, "address": {"country": "USA", "city": "New York"}}';

print_r (json_decode($json));
?>

#output: stdClass Object
(
    [name] => Leo
    [age] => 28
    [address] => stdClass Object
        (
            [country] => USA
            [city] => New York
        )

)

See also