PHP and JavaScript are actually very convenient, and PHP also provides native support for JSON format. Mainly includes two functions: JSON encoding and decoding:
json_endoce: http://cn.php.net/json_encode
json_dedoce: http://cn.php.net/json_decodejson_encode — JSON encodes the variable and returns the JSON form of value, for example:
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
Output after executing the above code:
{"a":1,"b":2,"c":3,"d":4,"e":5}
If the data source to be encoded (usually an array) contains Chinese in the value, the output after json_encode processing is unicode encoding.
$arr = array ('a'=>'Mango Station');
echo json_encode($arr);
?>
Output after executing the above code:
{"a":"u8292u679Cu5C0Fu7AD9"}
The bottom layer of PHP has already done unicode processing. If you find it not intuitive enough, you can use the urlencode and urldecode methods to bypass the process of transcoding to unicode:
$arr = array ('a'=>urlencode('Mango Station'));
echo urldecode(json_encode($arr)); After the above code is executed, the output is:
{"a":"Mango Station"}