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


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

    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);
  • 相关阅读:
    浏览器渲染原理
    react ssr 从0到1实践
    阿里的上线水准
    http各个版本(1/1.1/2)对比
    可监控、可灰度、可回滚为什么会成为挑战?
    HTTP1、HTTP1.1、http2、http3的区别
    关于队头阻塞(HeadofLine blocking),看这一篇就足够了
    document.ready和window.onload的区别
    HTTP1和HTTP2的区别
    html,css,js加载顺序
  • 原文地址:https://www.cnblogs.com/bafeiyu/p/4329629.html
Copyright © 2020-2023  润新知