Current Location: Home> Function Categories> json_encode

json_encode

JSON encoding of variables
Name:json_encode
Category:JSON
Programming Language:php
One-line Description:Encode the value into JSON format.

Definition and usage

The json_encode() function is used to encode values ​​into JSON format.

Example

Example 1

How to encode an associative array into a JSON object:

 <?php
$age = array ( "Bill" => 35 , "Elon" => 37 , "Steve" => 43 ) ;

echo json_encode ( $age ) ;
?>

Run the instance

Example 2

How to encode an index array into a JSON array:

 <?php
$cars = array ( "Volvo" , "BMW" , "Toyota" ) ;

echo json_encode ( $cars ) ;
?>

Run the instance

grammar

 json_encode ( value , options , depth )
parameter describe
value Required. Specifies the value to encode.
options Optional. Specify bitmasks (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_UNESC APED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR)
depth Optional. Specify the maximum depth.
Similar Functions
Popular Articles