AppDividend
Latest Code Tutorials

How To Convert PHP Array To JSON Tutorial With Example

0

In this tutorial, we will see How To Convert PHP Array To JSON. Everyday use of JSON is to read data from a web server and display the data on a web page. When exchanging data between the browser and a server, data can only be in the form of text. JSON is text format, and we can convert any JavaScript object into a JSON format, and send that JSON to a server. We can also convert any JSON received from a server into JavaScript objects.

Convert PHP Array To JSON

PHP has some built-in functions to handle JSON. Objects and Array in PHP can be converted into JSON by using the PHP function json_encode(). The PHP json_encode() function returns the string containing a JSON equivalent of the value passed to it as we demonstrate here with the numerically indexed array.

Let’s see the following example.

<?php

// app.php

$netflix = ['Black Mirror', '13 Reasons Why', 'Bird Box', 'Dirt'];

$netJSON = json_encode($netflix);

echo $netJSON."\n";

The output is following.

 

Convert PHP Array To JSON

A numerically indexed PHP array is translated to the array literal in a JSON string.  JSON_FORCE_OBJECT option can be used if you want that array to be output as the object instead.

See the following code.

<?php

// app.php

$netflix = ['Black Mirror', '13 Reasons Why', 'Bird Box', 'Dirt'];
$netJSON = json_encode($netflix, JSON_FORCE_OBJECT);
echo $netJSON."\n";

The output is following.

 

Convert PHP Array To JSON Tutorial With Example

Convert Associative Array to JSON

Let’s take an example of converting a key-value pair array to json.

<?php

// app.php

$data = ['name' => 'Krunal', 'blog' => 'AppDividend', 'education' => 'BE'];
$jsonData = json_encode($data);
echo $jsonData."\n";

The output of the code is following.

 

Convert Associative Array to JSON

The json_encode() function returns a JSON encoded string on success or FALSE on failure.

json_encode() example showing some options

See the following code.

<?php

// app.php

$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");

echo "Normal: ",  json_encode($a), "\n";
echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ",    json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";

$b = array();

echo "Empty array output as array: ", json_encode($b), "\n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";

$c = array(array(1,2,3));

echo "Non-associative array output as array: ", json_encode($c), "\n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";

$d = array('foo' => 'bar', 'baz' => 'long');

echo "Associative array always output as object: ", json_encode($d), "\n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";

In the above code, we have passed an options parameter to the json_encode() function.

The various options are Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK,JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION,JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_UNESCAPED_LINE_TERMINATORS,JSON_THROW_ON_ERROR. 

The output of the above code is following.

 

How To Convert PHP Array To JSON Tutorial With Example

Finally, How To Convert PHP Array To JSON Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.