In this tutorial, you will learn how to convert an array to JSON and JSON to an array in PHP.
PHP has two inbuilt functions json_encode()
and json_decode()
, with the help of these functions you can convert an array to JSON and JSON to an array.
PHP json_encode() Function
The PHP json_encode()
function allows you to convert an associative array to JSON.
This function takes three parameters, and it returns a JSON encoded string on success, else it will return False.
json_encode(Value, Options, Depth)
Value
(required) – In order to convert an array to JSON, here you have to put an associative array. But this parameter can take any type of value except a resource.Options
(optional) – This parameter takes a predefined constant or an integer to specify a bitmask. Such as if you want to return a prettified JSON, you have to passJSON_PRETTY_PRINT
or128
.Depth
(optional) – It takes an integer to specify the maximum depth, and it must be greater than zero.
<?php
$array = [
"id" => 1,
"name" => "John Doe",
"email" => "[email protected]"
];
$jsonData = json_encode($array);
echo $jsonData;
{"id":1,"name":"John Doe","email":"[email protected]"}
Options parameter example
<?php
$array = [
"id" => 1,
"name" => "John Doe",
"email" => "[email protected]"
];
$jsonData = json_encode($array, JSON_PRETTY_PRINT);
echo "<pre>$jsonData</pre>";
{
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
Check out more about PHP JSON constants.
<?php
echo json_encode(["Tags" => "<foo>"], JSON_HEX_TAG)."\n";
echo json_encode(["Apos" => "I'm"], JSON_HEX_APOS)."\n";
echo json_encode(["Quot" => "I'm"], JSON_HEX_QUOT)."\n";
echo json_encode(["Amp" => "Kings & Queens"], JSON_HEX_AMP)."\n";
echo json_encode(["Unicode" => "\xC2\xB6"], JSON_UNESCAPED_UNICODE)."\n";
echo json_encode(["Force Object"], JSON_FORCE_OBJECT)."\n";
{"Tags":"\u003Cfoo\u003E"}
{"Apos":"I\u0027m"}
{"Quot":"I'm"}
{"Amp":"Kings \u0026 Queens"}
{"Unicode":"¶"}
{"0":"Force Object"}
Depth parameter example
In the following code, you can see the array’s depth is 2, but 1 is specified as the maximum depth in the json_encode()
function, and that’s why it returned false.
If you don’t want to care about the depth, just leave this parameter. By default, it is 512.
<?php
$array = [
"id" => 1,
"name" => "John Doe",
"email" => "[email protected]",
"call" => ["+1-555-5649-321", "+1-555-3538-202", "+1-555-6757-702"]
];
$jsonData = json_encode($array, JSON_PRETTY_PRINT, 1);
var_dump($jsonData);
bool(false)
PHP json_encode() Function
With the help of the json_decode()
function, you can convert a JSON string to PHP array.
json_decode(String, Assoc, Depth, Options)
String
(required) – Specify the JSON string you want to decode or convert to an array.Assoc
(optional) – By default, it is FALSE. But in order to return an associative array, you must pass TRUE otherwise, it will return a PHP object.Depth
(optional) – Maximum nesting depth of the structure being decoded.Options
(optional) – Takes a predefined JSON constant or an integer to specify a bitmask.- This function will return NULL if the given JSON string cannot be decoded.
<?php
$json = '{"id":1,"name":"John Doe","email":"[email protected]"}';
$array = json_decode($json, true);
echo "<pre>";
print_r($array);
echo "</pre>";
Array
(
[id] => 1
[name] => John Doe
[email] => [email protected]
)