The function for converting php array to json is json_encode(). The json_encode() function can JSON encode variables and convert them into json string data with the syntax format "json_encode (value)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
How to convert php array to json? You can use the json_encode() function.
Example:
<?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); ?>
Output:
{"a":1,"b":2,"c":3,"d":4,"e":5}
PHP json_encode() function
json_encode() function can Variables are JSON encoded
Syntax:
json_encode ( $value [, $options = 0 ] )
Returns a string containing the representation of value in JSON form.
Note:
1. $value is the value to be encoded, and this function is only valid for UTF8 encoded data;
2. options: consists of the following constants Binary masks: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT;
3. The second parameter is generally not needed;
4. JSON data is actually a string , you can use var_dump() to print it out to see the data type;
5. If the execution is successful, JSON data will be returned, otherwise FALSE will be returned.
Return value:
Returns a JSON encoded string on success or false on failure.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the function to convert php array to json?. For more information, please follow other related articles on the PHP Chinese website!