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

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically