Change language

PHP Json_encode () function

Function json_encode()- it is a built-in function in PHP that is used to convert a PHP array or object to JSON representation. 
Syntax:
string json_encode ($value, $option, $depth)
Parameters :
  • $value:this is a required parameter that defines the value to be encoded.
  • $option: This is an optional parameter that determines the bit mask 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:is an optional parameter that sets the maximum depth. Its value must be greater than zero.
Return Value:This function returns a JSON representation on success, or false on error.Example 1:This example encodes a PHP array into JSON representation.  
// Declare the array $value = array ( "name" = > " GFG " , "email" = > "[email protected]" ) ;  
// Using the json_encode() function $json = json_encode ( $value );  
// Show output echo ( $json ) ;  
?>
Exit:
{"name ":" GFG "," email ":" [email protected] "}
Example 2:This example encodes a PHP multidimensional array to JSON representation.  
// Declare a multidimensional array $value = array ( "name" = > "GFG" , array ( "email" = > "[email protected]" , "mobile" = > "XXXXXXXXXX" ) );  
// Using the json_encode() function $json = json_encode ( $value );  
// Show output echo ( $json ) ;  
?>
Exit:
{"name ":" GFG "," 0 ": {" email ":" [email protected] "," mobile ":" XXXXXXXXXX "}}
Example 3:This the example encodes PHP objects into JSON representation.  
// Declare the class class GFG {  
} 
// Declare the object $value = new GFG();  
// Set object elements $value -> organization = "GeeksforGeeks" ; $value -> email = "[email protected]" ;  
// Using the json_encode() function $json = json_encode ( $value );  
// Show output echo ( $json ) ;  
?>
Exit:
{"organization ":" GeeksforGeeks "," email ":" [email protected] "}
Link: https://www.php.net/manual/en/function.json-encode.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method