一丶注意事项
- 直接用json编码array可能会返回null,因为有中文
- php5.2以上的版本才可以使用
- js端接收也是utf8编码
二丶思路
首先将array进行urlencode
然后将array转成json
最后将json进行urldecode
三丶上代码 最后调用 ArrayToJson($array) 即可
<?php
//干掉json中文
function ArrayUrlencode($array, $function, $apply_to_keys_also = false)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
die('possible deep recursion attack');
}
foreach ($array as $key => $value) {
if (is_array($value)) {
ArrayUrlencode($value, $function, $apply_to_keys_also);
} else {
$array[$key] = $function($value);
}
if ($apply_to_keys_also && is_string($key)) {
$new_key = $function($key);
if ($new_key != $key) {
$array[$new_key] = $array[$key];
unset($array[$key]);
}
}
}
$recursive_counter--;
}
function ArrayToJson($array) {
ArrayUrlencode($array, 'urlencode', true);
$json = json_encode($array,JSON_UNESCAPED_UNICODE);
return urldecode($json);
}