AppDividend
Latest Code Tutorials

How To Convert PHP Array To JSON Example

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 a text format, and we can convert any JavaScript object into a JSON format and send that JSON to a server.

PHP has some built-in functions to handle JSON.

Convert PHP array to json

To convert PHP array to JSON, use the json_encode() function. PHP json_encode() is an inbuilt function that converts PHP array to json. The json_encode() function returns the string containing a JSON equivalent of the value passed to it, as we demonstrate with the numerically indexed array. We can also convert any JSON received from a server into JavaScript objects.

Objects and Array in PHP can be converted into JSON by using the PHP json_encode().

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";

See the output

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";

See the output.

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";

See the output

Convert Associative Array to JSON

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

PHP 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

That’s it for this tutorial.

1 Comment
  1. Paramjeet Singh says

    Thanks. This post does a lot of help.

Leave A Reply

Your email address will not be published.

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