• 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"=>"小鱼儿"

    ),
    2=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ),
    3=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ),
    4=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ),
    5=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ),
    6=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ),
    7=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ),
    8=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ),
    9=>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"=>1362105283,
    "wangwang"=>"芭菲雨"

    ));

    $str=JSON($guanggao_list);

    print_r($str);

  • 相关阅读:
    iOS开发日记37-app加急审核
    iOS开发日记36-iOS中的依赖注入
    iOS开发日记35-事件处理机制与图像渲染过程
    iOS开发日记34-国际化
    iOS开发日记33-hit-Test
    iOS开发日记32-详解多线程(死锁)
    iOS开发日记31-详解Block
    iOS开发日记30-Autolayout
    springMVC js等文件找不到解决方法
    jQuery UI 中Tabs Ajax载入时出现Http 304的问题
  • 原文地址:https://www.cnblogs.com/bafeiyu/p/2942275.html
Copyright © 2020-2023  润新知