JSON is the best format to transfer data over the network. It is an easily parsable format comparatively. That’s why most API accepts parameters and returns responses in JSON.
There are online tools to convert an array to a JSON object. This tutorial teaches how to create a program to convert various types of PHP array input into a JSON format.
It has four different examples of converting a PHP array to JSON. Those are too tiny in purpose to let beginners understand this concept quickly.
This quick example is coded with a three-line straightforward solution. It takes a single-dimensional PHP array and converts it to JSON.
<?php
$array = array(100, 250, 375, 400);
$jsonString = json_encode($array);
echo $jsonString;
?>
The other different array-to-JSON examples handle simple to complex array conversion. It also applies pre-modification (like array mapping) before conversion. The four examples are,
If you want the code for the reverse to decode JSON objects to an array, then the linked article has examples.
See this online demo to convert an array of comma-separated values into a JSON object.
This code handles three types of array data into a JSON object. In PHP, it is effortless to convert an array to JSON.
It is a one-line code using the PHP json_encode() function.
<?php
// PHP Array to JSON string conversion for
// simple, associative and multidimensional arrays
// all works the same way using json_encode
// just present different arrays for example purposes only
// simple PHP Array to JSON string
echo '<h1>PHP Array to JSON</h1>';
$array = array(
100,
250,
375,
400
);
$jsonString = json_encode($array);
echo $jsonString;
// Associative Array to JSON
echo '<h2>Associative PHP Array to JSON</h2>';
$array = array(
'e1' => 1000,
'e2' => 1500,
'e3' => 2000,
'e4' => 2350,
'e5' => 3000
);
$jsonString = json_encode($array);
echo $jsonString;
// multidimensional PHP Array to JSON string
echo '<h2>Multidimensional PHP Array to JSON</h2>';
$multiArray = array(
'a1' => array(
'item_id' => 1,
'name' => 'Lion',
'type' => 'Wild',
'location' => 'Zoo'
),
'a2' => array(
'item_id' => 2,
'name' => 'Cat',
'type' => 'Domestic',
'location' => 'Home'
)
);
echo json_encode($multiArray);
?>
Output:
//PHP Array to JSON
[100,250,375,400]
//Associative PHP Array to JSON
{"e1":1000,"e2":1500,"e3":2000,"e4":2350,"e5":3000}
//Multidimensional PHP Array to JSON
{"a1":{"item_id":1,"name":"Lion","type":"Wild","location":"Zoo"},"a2":{"item_id":2,"name":"Cat","type":"Domestic","location":"Home"}}
This code handles a different scenario of JSON conversion, which must be helpful if needed. For example, if the array associates subject=>marks and the user needs only the marks to plot it in a graph.
It removes the user-defined keys from an associative array and applies json_encode to convert it. It is a two-step process.
<?php
// array_values() to remove assigned keys and convert to the original PHP Array key
echo '<h1>To remove assigned associative keys and PHP Array to JSON</h1>';
$array = array(
'e1' => 1000,
'e2' => 1500,
'e3' => 2000,
'e4' => 2350,
'e5' => 3000
);
$jsonString = json_encode(array_values($array));
echo $jsonString;
?>
Output:
[1000,1500,2000,2350,3000]
It is also a two-step process to convert the array of data containing accented characters.
It applies UTF8 encoding on the array values before converting them into a JSON object.
It maps the utf8_encode() as a callback using the PHP array_map() function to encode all the elements of the given array.
We have seen PHP array functions frequently used while working with arrays.
<?php
// Accented characters
// to preserve accented characters during PHP Array to JSON conversion
// you need to utf8 encode the values and then do json_encode
echo '<h1>For accented characters PHP Array to JSON</h1>';
$array = array(
'w1' => 'résumé',
'w2' => 'château',
'w3' => 'façade',
'w4' => 'déjà vu',
'w5' => 'São Paulo'
);
$utfEncodedArray = array_map("utf8_encode", $array);
echo json_encode($utfEncodedArray);
?>
Output:
{"w1":"r\u00c3\u00a9sum\u00c3\u00a9","w2":"ch\u00c3\u00a2teau","w3":"fa\u00c3\u00a7ade","w4":"d\u00c3\u00a9j\u00c3\u00a0 vu","w5":"S\u00c3\u00a3o Paulo"}
It applies to prettyprint on the converted output JSON properties in a spacious format.
The PHP json_encode() function accepts the second parameter to set the bitmask flag. This flag sets the JSON_PRETTY_PRINT to align the output JSON properties.
<?php
// to neatly align the output with spaces
// it may be useful when you plan to print the
// JSON output in a raw format
// helpful when debugging complex multidimensional PHP Arrays and JSON objects
// lot more constants are available like this, which might be handy in situations
echo '<h1>Convert PHP Array to JSON and Pretty Print</h1>';
$array = array(
'e1' => 1000,
'e2' => 1500,
'e3' => 2000,
'e4' => 2350,
'e5' => 3000
);
echo json_encode($array, JSON_PRETTY_PRINT);
?>
Output:
{ "e1": 1000, "e2": 1500, "e3": 2000, "e4": 2350, "e5": 3000 }