php

How to Convert JSON String to Array in PHP

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is used to transmit data between a server and a web application or between different systems.

If you are working with JSON data in your PHP application, you may need to convert it to an array for easier processing. Converting JSON to an array is a common task in web development and can be done using built-in PHP functions.

In this article, we will explore different methods for converting JSON string to array in PHP.

How to Convert JSON String to Array in PHP

A JSON is converted to a PHP array by decoding the JSON string using the json_decode() function. A PHP function called json_decode() accepts a JSON string as input and produces a PHP object. To use this function, you need to pass the JSON string as the first argument and set the second argument to true, which tells PHP to convert the JSON string to a PHP associative array. After decoding the JSON text, you may alter the data using the resultant PHP array.

The syntax for json_decode() function is:

json_decode(
    string $json,
    ?bool $assoc = null,
    int $depth = 512,
    int $flags = 0
): mixed

The arguments passed to this function are:

  • json: The JSON string that was supplied to the PHP variable for decoding. Only UTF-8 encoded strings are compatible with this method.
  • assoc: The variable is Boolean. When the JSON_OBJECT_AS_ARRAY flag is set to true, the JSON object will be converted into an associative array; when the flag is set to false, the JSON object will be delivered as a stdClass object; and when the NULL flag is set, the JSON object will either be returned as an associative array or an object.
  • depth: Maximum depth of the JSON that needs to be decoded.
  • flags: It contains bitmasks for the JSON constants JSON_OBJECT_AS_ARRAY (convert JSON objects to an associative array), JSON_BIGINT_AS_STRING (convert large integers to strings), JSON_THROW_ON_ERROR (throw error), and others.

Return Value

According to the parameter, the json_decode() function converts the JSON string to the proper PHP array. Null is returned if the JSON could not be decoded or if it is deeper than the specified depth.

Let’s look at a code example:

<?php
$json_string = '{"name":"Ira", "age":24, "city":"California"}';
$array = json_decode($json_string, true);
print_r($array);
?>

The above code generates a JSON string containing information about a person’s name, age, and city, and then converts that JSON string into a PHP array using PHP’s json_decode() function. A variable called $array holds the output array. The name, age, and city of the person are displayed as an associative array on the screen by printing the contents of the $array variable using the print_r() method.

The json_decode() function can also be used with nested JSON and custom options.

Using json_decode() Function with Nested JSON

JSON strings can be structured in different ways, such as arrays or nested objects. If the JSON string contains nested objects, you can use a recursive function to convert each nested object to a PHP array. You can also use the json_decode() function to convert the nested objects to PHP arrays recursively, by setting the second parameter assoc to true. This will make sure that the JSON string’s whole contents are converted to a PHP array.

Let’s look at a code example:

<?php
$json_string = '{"name":"Ira", "age":24, "city":"California", "friends":["Frank", "Ahyan", "Martin"]}';
$array = json_decode($json_string, true);
print_r($array);
?>

The above program generates a JSON string with information about a person’s name, age, city, and list of friends. The JSON string is then transformed into a PHP array using the json_decode() function, which returns an associative array when the second parameter is set to true.

A variable called $array holds the final PHP array. The $array variable’s contents, including the person’s name, age, and city as well as an array of their friends, are then shown on the screen using the print_r() method.

Using json_decode() Function with Custom Options

Additional settings can be specified by passing a third parameter to the json_decode() function. For instance, by setting the JSON_BIGINT_AS_STRING option, large integers will be handled as strings rather than converted to floats.

Let’s look at a code example:

<?php
$json_string = '{"id": 4634693493743705}';
$array = json_decode($json_string, true, 512, JSON_BIGINT_AS_STRING);
print_r($array);
?>

This program constructs a JSON string with a huge integer value for the id field and then converts the JSON string into a PHP array using the PHP function json_decode(). The huge integer value is returned as a string rather than being cast to a float or scientific notation using the JSON_BIGINT_AS_STRING fourth parameter of the json_decode() function. A variable called $array holds the final PHP array.

Conclusion

Web development frequently requires converting JSON strings to arrays, and PHP’s built-in json_decode() function does just that. A JSON string may be turned into a PHP associative array by giving it to the function while setting the second argument to true. The function can also handle nested JSON objects and allows for custom options to be specified through its parameters.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.