Updated March 30, 2023
Introduction to PHP json_encode
There are built-in functions to handle JSON and the function used to convert the objects to JSON is called the json_encode function in PHP. And JSON is something that is used to read the data from the webserver and display the read data on a web page. When the json_encode function is used to encode the PHP value in the format of JSON, the json_encode function returns the encoded string in JSON if it is successful and FALSE is returned by the json_encode function if it is a failure and this json_encode function is available in all the PHP versions after version 5.2.
Syntax:
json_encode(value, options, depth)
where value is the value that is to be encoded in JSON format, a bitmask is specified by options and this is optional and maximum depth is specified by the depth and this is optional.
Working of PHP json_encode Function
Working of json_encode function is as follows:
- Whenever there is a need to handle JSON in PHP using built-in functions, one of the functions that can be used is the json_encode() function.
- JSON is something that is used to read the data from the webserver and display the read data on a web page.
- The function used to convert the objects to JSON is called the json_encode function in PHP.
- When the json_encode function is used to encode the value in the format of JSON, the json_encode function returns the encoded string in JSON if it is successful, and FALSE is returned by the json_encode function if it is a failure.
- The json_encode function is available in all versions after version 5.2.
Examples
Following are the examples are given below:
Example #1
PHP program to demonstrate json_encode program to encode the given PHP array into JSON array:
Code:
<?php
// a PHP array is declared and the capital cities of the states of India is stored in an array which is represented by a variable
$val = array(
"Karnataka"=>"Bangalore",
"Goa"=>"Panaji",
"Gujarat"=>"Gandhinagar",
"Haryana"=>"Chandigarh",
"Bihar"=>"Patna");
// json_encode() function is used to convert the PHP array into JSON array
$object = json_encode($val);
// the converted PHP array is displayed in JSON array on the screen
echo($object);
?>
Output:
In the above program, a PHP array is declared and the capital cities of the states of India are stored in this array which is represented by a variable. Then json_encode() function is used to convert the PHP array into JSON representation. Then the converted PHP array is displayed in JSON representation as to the output on the screen.
Example #2
PHP program to demonstrate json_encode program to encode the given indexed array into JSON array:
Code:
<?php
// a PHP indexed array is declared and the statements to be converted into JSON array is stored in an array which is represented by a variable
$val = array(
"Welcome","to",
"PHP","Concept",
"is","json_encode",
"function","Learning",
"is","fun");
// json_encode() function is used to convert the PHP array into JSON array
$object = json_encode($val);
// the converted PHP array is displayed in JSON array on the screen
echo($object);
?>
Output:
In the above program, a PHP array is declared and the statements to be converted into JSON array is stored in this array which is represented by a variable. Then json_encode() function is used to convert the array into JSON array. Then the converted array is displayed in JSON array as the output on the screen. The output is shown in the snapshot above.
Example #3
PHP program to demonstrate json_encode program to encode the given multi dimensional array into JSON array:
Code:
<?php
// a multi dimensional PHP array is declared and the author of the book, name of the book and the publishing year of the book is stored in a multi dimensional array which is represented by a variable
$val = array(
"Author_name"=>"ShobhaShivakumar",
array(
"Book_name"=>"Welcome to PHP",
"Publishing_year"=>"2020"
)
);
// json_encode() function is used to convert the PHP multi dimensional array into JSON array
$object = json_encode($val);
// the converted multi dimensional PHP array is displayed in JSON array on the screen
echo($object);
?>
Output:
In the above program, a multidimensional array is declared and the author of the book, name of the book, and the publishing year of the book is stored in a multidimensional array which is represented by a variable. Then json_encode() function is used to convert the multidimensional array into JSON array. Then the converted multidimensional array is displayed in JSON array as the output on the screen. The output is shown in the snapshot above.
Example #4
PHP program to demonstrate json_encode program to covert the given objects into JSON objects:
Code:
<?php
//an object is created in PHP to store two messages
$firstobject->Message1 = "Welcome to PHP";
$firstobject->Message2 = "Learning is fun";
//json_encode() function is used to convert the objects in PHP to JSON object
$Convertedobject = json_encode($firstobject);
//the converted object is displayed on the screen
echo $Convertedobject;
?>
Output:
In the above program, the object is created to store two messages. Then json_encode() function is used to convert the objects in PHP to JSON object. Then the converted object is displayed on the screen. The output is shown in the snapshot above.
Recommended Articles
This is a guide to PHP json_encode. Here we also discuss the introduction and working of the function along with different examples and its code implementation. You may also have a look at the following articles to learn more –