Home >Backend Development >PHP Tutorial >Usage of json_decode() and json_encode() in php and solution for Chinese not showing_PHP tutorial
This article introduces the usage of json_decode() and json_encode() in php and the solution for not displaying Chinese characters. Friends in need can refer to it.
json_decode() and json_encode() in php
1.json_decode()
json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_decode — Encode a JSON-formatted string
Description
mixed json_decode ( string $json [, bool $assoc ] )
Accepts a JSON formatted string and converts it into a PHP variable
Parameters
json
The string in json string format to be decoded.
assoc
When this parameter is TRUE, an array is returned instead of an object.
Return value
Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.
Example
Example #1 Example of json_decode()
The code is as follows | Copy code | ||||||||
|
The code is as follows | Copy code |
echo json_decode($data,true); Result: Array ( [0] => Array ( [Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [1] => Array ( [ Name] => a1 [Number] => 123 [Contno] => 000 [QQNo] => ) [2] => Array ( [Name] => a1 [Number] => 123 [ Contno] => 000 [QQNo] => ) ) |
You can see that json_decode($data,true) outputs an associative array. From this, we can see that json_decode($data) outputs an object, and json_decode("$arr",true) forces it to generate a PHP associative array. .
2.json_encode()
json_encode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_encode — JSON encode variables
Report a bug Description
string json_encode ( mixed $value [, int $options = 0 ] )
Returns the JSON form of value
Report a bug parameter
value
The value to be encoded can be any data type except resource type
This function can only accept UTF-8 encoded data
options
Binary mask consisting of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE.
Report a bug return value
Returns a string representation in JSON format on success or FALSE on failure.
Report a bug update log
Version Description
5.4.0 options parameter adds constants: JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE.
5.3.3 The options parameter adds a constant: JSON_NUMERIC_CHECK.
5.3.0 Add options parameter.
Report a bug Example
Example #1 A json_encode() example
The code is as follows | Copy code |
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); The above routine will output: {"a":1,"b":2,"c":3,"d":4,"e":5} Example #2 Usage of options parameter in json_encode() function
$a = array(' echo "Normal: ", json_encode($a), "n"; $b = array(); echo "Empty array output as array: ", json_encode($b), "n"; $c = array(array(1,2,3)); echo "Non-associative array output as array: ", json_encode($c), "n"; $d = array('foo' => 'bar', 'baz' => 'long'); echo "Associative array always output as object: ", json_encode($d), "n"; The above routine will output: Normal: [" Empty array output as array: [] Non-associative array output as array: [[1,2,3]] Associative array always output as object: {"foo":"bar","baz":"long"} |
Example #3 Example of continuous and non-continuous arrays
The code is as follows
|
Copy code
|
||||||||||||||||||||||||
var_dump( );
|