• json_encode 转化数组时,中文不转义出现乱码的解决方法


         一个简单的方法,就是在json_encode($data) ,后面添加一个参数   json_encode($data, JSON_UNESCAPED_UNICODE);

    写一个简单的代码

    public function appLog($id,$age)
    {
    $User = M('User');
    $where['id'] = $id;
    $where['age'] = $age;
    $data = $User->where($where)->select();
    if ($data){
    return $this->api_return('请求成功!',0,$data);
    }else{
    return $this->api_return('请求失败!',-1,'null');
    }

    }

    public function api_return($msg = '', $code = -1, $data = null)
    {
    $output = array(
    'message' => $msg,
    'code' => $code,//-1表示错误 0 表示正确返回数据
    'data' => $data
    );
    // 返回JSON数据格式到客户端
    header('Content-Type:application/json; charset=utf-8');
    $output = json_encode($output, JSON_UNESCAPED_UNICODE);
    echo $output;

    }


    {"message":"请求成功!","code":0,"data":[{"id":"2","name":"sohu","age":"19","email":"zzy@souhu.com","status":"1","sex":"1"}]}
  • 相关阅读:
    hdu4020简单想法题
    hdu4020简单想法题
    hdu4284 dfs+floyd
    hdu4284 dfs+floyd
    hdu4282 x^z+y^z+x*y*z=k 解的个数
    hdu4282 x^z+y^z+x*y*z=k 解的个数
    hdu4279 找规律+小想法
    hdu4279 找规律+小想法
    hdu3665 水最短路
    hdu3665 水最短路
  • 原文地址:https://www.cnblogs.com/haoxuanchen2014/p/7768088.html
Copyright © 2020-2023  润新知