JSON_UNESCAPED_UNICODE和JSON_UNESCAPED_SLASHES

php格式化json的函数,json_encode($value, $options);

2个比较常用的参数:

1、JSON_UNESCAPED_UNICODE(中文不转为unicode,对应的数字256)

$array = ['a'=>'床前明月光/', 'b'=>'疑是地上霜', 'c'=>'举头望明月', 'd'=>'低头思故乡'];
$arrayJson = json_encode($array, JSON_UNESCAPED_UNICODE);
var_dump($arrayJson);
// 结果 string(91) "{"a":"床前明月光\/","b":"疑是地上霜","c":"举头望明月","d":"低头思故乡"}"

2、JSON_UNESCAPED_SLASHES(不转义反斜杠,对应的数字64)

$array = ['a'=>'床前明月光/', 'b'=>'疑是地上霜', 'c'=>'举头望明月', 'd'=>'低头思故乡'];
$arrayJson = json_encode($array, JSON_UNESCAPED_SLASHES);
var_dump($arrayJson);
// 结果 string(150) "{"a":"\u5e8a\u524d\u660e\u6708\u5149/","b":"\u7591\u662f\u5730\u4e0a\u971c","c":"\u4e3e\u5934\u671b\u660e\u6708","d":"\u4f4e\u5934\u601d\u6545\u4e61"}"

2个参数同时使用:

JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES = 320

使用方法:json_encode($array, 320);

$array = ['a'=>'床前明月光/', 'b'=>'疑是地上霜', 'c'=>'举头望明月', 'd'=>'低头思故乡'];
$arrayJson = json_encode($array, 320);
var_dump($arrayJson);
// 结果 string(90) "{"a":"床前明月光/","b":"疑是地上霜","c":"举头望明月","d":"低头思故乡"}"

 

你可能感兴趣的:(PHP,json_encode)