PHP | json_encode() Function

The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
Syntax :

string json_encode( $value, $option, $depth )

Parameters:

  • $value: It is a mandatory parameter which defines the value to be encoded.
  • $option: It is optional parameter which defines the Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT, 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.
  • $depth: It is optional parameter which sets the maximum depth. Its value must be greater than zero.

Return Value: This function returns a JSON representation on success or false on failure.

Example 1: This example encodes PHP array into JSON representation.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Declare an array 
$value = array(
    "name"=>"GFG",
    "email"=>"abc@gfg.com");
   
// Use json_encode() function
$json = json_encode($value);
   
// Display the output
echo($json);
   
?>

chevron_right


Output:

{"name":"GFG","email":"abc@gfg.com"}

Example 2: This example encodes PHP multidimensional array into JSON representation.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare multi-dimensional array 
$value = array(
    "name"=>"GFG",
    array(
        "email"=>"abc@gfg.com",
        "mobile"=>"XXXXXXXXXX"
    )
);
   
// Use json_encode() function
$json = json_encode($value);
   
// Display the output
echo($json);
   
?>

chevron_right


Output:

{"name":"GFG","0":{"email":"abc@gfg.com","mobile":"XXXXXXXXXX"}}

Example 3: This example encodes PHP objects into JSON representation.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare class
class GFG {
       
}
   
// Declare an object
$value = new GFG();
   
// Set the object elements
$value->organisation = "GeeksforGeeks";
$value->email = "feedback@geeksforgeeks.org";
  
// Use json_encode() function
$json = json_encode($value);
   
// Display the output
echo($json);
  
?>

chevron_right


Output:

{"organisation":"GeeksforGeeks","email":"feedback@geeksforgeeks.org"}

Reference: https://www.php.net/manual/en/function.json-encode.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.