Syntax
The first argument of this function is mandatory and the other two arguments are optional. It returns JSON data if the data is converted into JSON format properly, otherwise returns false.
The first argument contains the value that will be encoded in JSON format.
The second argument is to set the bitmask value at the time of conversion.
JSON_HEX_TAG, JSON_HEX_AMP,
JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE,
JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK,
JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION,
JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS,
JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE,
JSON_THROW_ON_ERROR.
The third argument is used to set the maximum depth that will be greater than 0.
Uses of json_encode() Function
The ways to convert different types of data into JSON format has shown in this part of the tutorial by using different examples.
Example-1: Convert PHP Array into JSON Data
Create a PHP file with the following script to know the way of converting an associative array into JSON data. An associative array of 4 elements has been declared in the script. The array values and the converted JSON values will be printed.
$jsonData = json_encode($student);
//Print the JSON data
echo "<br/><b>The JSON values are:</b><br/>". $jsonData;
?>
Output:
The following output will appear after executing the above script.
Example-2: Convert an Array into JSON Data Using JSON_NUMERIC_CHECK
Create a PHP file with the following script that will convert the array into JSON data by changing the numeric string values of the array into the numbers. The bitmask value, JSON_NUMERIC_CHECK has been used in the second argument to convert the numeric string values into the number. The array values and the converted JSON values will be printed later.
//Declare an array of different types of data
$mix_array = array('897', '4.53', 100, '-14', '2.5e3', 67, 'LinuxHint', True);
//Print the array values
echo "<b>The array values are:</b><br/>";
print_r($mix_array);
//Convert array to JSON data by checking numeric value
$jsonData = json_encode($mix_array, JSON_NUMERIC_CHECK);
//Print the JSON data
echo "<br/><br/><b>The JSON values are:</b><br/>". $jsonData;
?>
Output:
The following output will appear after executing the above script. In the output, four numeric string values have converted into a number and the Boolean value has converted into a number.
Example-3: Convert Array into JSON Data After Deleting the Array Value
Create a PHP file with the following script to check the way of converting array values into JSON data after deleting an element from the array. Here, an associative array of 4 elements has declared. The array has converted into JSON data before deleting any element and after deleting one element. The third element of the array has deleted in the script.
$assoc_array = array(101=>"Pen", 102=>"Pencil", 103=>"NoteBook", 104=>"Ruler");
//Print the array values
echo "<b>The array values are:</b><br/>";
print_r($assoc_array);
//Convert array to JSON data
$jsonData = json_encode($assoc_array);
//Print the JSON data
echo "<br/><br/><b>The JSON values are:</b><br/>". $jsonData;
unset($assoc_array[103]);
//Print the array values
echo "<br/><br/><b>The array values after delete are:</b><br/>";
print_r($assoc_array);
//Convert array to JSON data
$jsonData = json_encode($assoc_array);
//Print the JSON data
echo "<br/><br/><b>The JSON values of the modified array are:</b><br/>". $jsonData;
?>
Output:
The following output will appear after executing the above script.
Example-4: Convert a Multi-Dimensional Array into JSON Data
Create a PHP file with the following script to know the way of converting a multi-dimensional associative array into JSON data. The values of the array and converted JSON data of the array will be printed in the output.
//Declare a multi-dimensional array
$courses = array(
"department" =>"CSE",
"semester" =>2,
array(
"CSE-202" =>"C Programming",
"CSE-203" =>"Digital Logic Design",
"CSE-204" =>"Algorithm",
"Math-102"=>"Mathematics"
)
);
//Print the array values
echo "<b>The array values are:</b><br/>";
print_r($courses);
//Convert array to JSON data
$jsonData = json_encode($courses);
//Print the JSON data
echo "<br/><br/><b>The JSON values are:</b><br/>". $jsonData;
?>
Output:
The following output will appear after executing the above script.
Example-5: Convert an Object into JSON Data
Create a PHP file with the following script to know the way of converting an object into JSON data. A class named testClass has been defined in the script that has 3 public variables. Next, the object variable has been declared and the class variables have initialized using the object. The object properties and the converted JSON values of the object will be printed in the output.
//Declare a simple class
class testClass {
public $var1;
public $var2;
public $var3;
}
//Declare an object of the class
$object = new testClass();
//Initialize the variables of the class
$object->var1 = "Good";
$object->var2 = "Better";
$object->var3 = "Best";
//Print the object properies
echo "<b>The object property values are:</b><br/>";
print_r($object);
//Convert the object into JSON data
$jsonData = json_encode($object);
//Print the JSON data
echo "<br/><br/><b>The JSON values are:</b><br/>". $jsonData;
?>
Output:
The following output will appear after executing the above script.
Example-6: Convert an Array into JSON Data Using JSON_PRESERVE_ZERO_FRACTION
Create a PHP file with the following script to convert the array of fractional data into JSON data by preserving the zero-fraction value.
The bitmask value, JSON_PRESERVE_ZERO_FRACTION has been used in the second argument of the json_encode() function to preserve the zero fractional values of the array. The array values and the JSON data with zero fraction and without zero fraction will be printed in the output.
//Declare an array of numeric data
$num_array = array(78.56, 80.0, 45.09, 51.0);
//Print the array values
echo "<b>The array values are:</b><br/>";
print_r($num_array);
//Convert array to JSON data
$jsonData = json_encode($num_array);
//Print the JSON data
echo "<br/><br/><b>The JSON values are:</b><br/>". $jsonData;
//Convert array to JSON data by using JSON_PRESERVE_ZERO_FRACTION
$jsonData = json_encode($num_array, JSON_PRESERVE_ZERO_FRACTION);
//Print the JSON data
echo "<br/><br/><b>The JSON values after preserving zero fraction are:</b><br/>". $jsonData;
?>
Output:
The following output will appear after executing the above script.
Conclusion
The ways to convert array values and object properties using the json_encode() function have been explained in this tutorial by using simple examples. I hope the PHP users will be able to use this function properly after reading this tutorial.