json不支持中文写入的问题解决

时间:2022-12-22 07:09:40

写两个函数,使其支持中文:

function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayRecursive($array[$key], $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]);
             }
        }
    }
}                    

 

function JSON($array) {
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    return urldecode($json);
}

 

调用示例如下

$guanggao_list=array(
0=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img01.taobaocdn.com/bao/uploaded/i1/13692019653157760/T1RedmXvBdXXXXXXXX_!!0-item_pic.jpg_310x310.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1362105283,
"wangwang"=>"芭菲雨"

),
1=>array(
"iid"=>21341872612,
"goods_name"=>"新款热销男装男士韩版修身裤休闲裤男裤 修身版休闲男装长裤子",
"goods_list_img"=>"http://img03.taobaocdn.com/imgextra/i3/745475881/T20n1JXmVbXXXXXXXX_!!745475881.jpg_460x460.jpg",
"url"=>"http://item.taobao.com/item.htm?spm=a230r.1.10.17.G7a5Tc&id=18937863889",
"shichangjia"=>"359.00",
"taobaojia"=>"199.00",
"daoqi"=>1372105283,
"wangwang"=>"小鱼儿"

)
);

$str=JSON($guanggao_list);

print_r($str);