PHP | json_encode() Function Last Updated : 17 Jun, 2019 Comments Improve Suggest changes Like Article Like Report 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. PHP <?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); ?> Output: {"name":"GFG","email":"abc@gfg.com"} Example 2: This example encodes PHP multidimensional array into JSON representation. PHP <?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); ?> Output: {"name":"GFG","0":{"email":"abc@gfg.com","mobile":"XXXXXXXXXX"}} Example 3: This example encodes PHP objects into JSON representation. PHP <?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); ?> Output: {"organisation":"GeeksforGeeks","email":"feedback@geeksforgeeks.org"} Reference: https://www.php.net/manual/en/function.json-encode.php Comment More infoAdvertise with us Next Article PHP | json_encode() Function aman neekhara Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | json_decode() Function The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable. Syntax: json_decode( $json, $assoc = FALSE, $depth = 512, $options = 0 ) Parameters: This function accepts four parameters as mentioned above and des 2 min read PHP | base64_encode() Function The base64_encode() function is an inbuilt function in PHP that is used to encode data with MIME base64. MIME (Multipurpose Internet Mail Extensions) base64 is used to encode the string in base64. The base64_encoded data takes 33% more space than the original data. Syntax: string base64_encode( $dat 1 min read PHP | iconv_get_encoding() Function The iconv_get_encoding() function is an inbuilt function in PHP which is used to retrieve the internal configuration variables of iconv extension. Syntax: mixed iconv_get_encoding( $type = "all" ) Parameters: This function accepts single parameter $type. The value of $type parameter are: all input_e 1 min read PHP | utf8_decode() Function The utf8_decode() function is an inbuilt function in PHP which is used to decode a UTF-8 string to the ISO-8859-1. This function decodes back to the encoded string which is encoded with the utf8_encode() function. Syntax:Â string utf8_decode( string $string ) Parameter: This function accepts single 2 min read PHP decoct( ) Function In the earlier days of computing, octal numbers and the octal numbering system was very popular for counting inputs and outputs because as it works in counts of eight, inputs and outputs were in counts of eight, a byte at a time. Due to a wide usage of octal number system many times it happens that 2 min read PHP | base64_decode() Function The base64_decode() is an inbuilt function in PHP which is used to Decodes data which is encoded in MIME base64.Syntax: string base64_decode( $data, $strict ) Parameters: This function accepts two parameter as mentioned above and described below: $data: It is mandatory parameter which contains the e 1 min read PHP | DsVector jsonSerialize() Function The Ds\Vector::jsonSerialize() function is an inbuilt function in PHP which is used to return the element which can be converted to JSON. Syntax: mixed public JsonSerializable::jsonSerialize( void ) Parameters: This function does not accepts any parameter. Return Value: This function returns the val 1 min read PHP | iconv() Function The iconv() function is an inbuilt function in PHP which is used to convert a string to requested character encoding. The iconv() is an international standard conversion application command-line programming interface which converts different character encodings to other encoding types with the help 3 min read PHP openssl_digest() Function The openssl_digest() function is an inbuilt function in PHP that is used to compute a digest hash value for the given data using a given method and returns a raw or binary hex-encoded string. Syntax: openssl_digest( string $data, string $digest_algo, bool $binary = false): string|false Parameters: T 2 min read PHP mb_encode_numericentity() Function The mb_encode_numercentity() function is an inbuilt function in PHP that is used to encode HTML entities with numerical values encoded using either decimal or hexadecimal representation. Syntax: string mb_encode_numericentity( string $string, array $map, string $encoding, bool $hex )Parameters: This 2 min read Like