zoukankan      html  css  js  c++  java
  • 【APP接口开发】php输出json格式数据

    请一定配合使用null转空字符的方法一起使用:(_unsetNull() 和 _json() 配合使用)

    在一些接口的调用中,直接查询数据库出来的字段可能为null字段,但是为了简便前端的判断,需要把null转换成空字符串'',这个时候就需要递归的方式进行。直接上代码如下:    
    
    
    //递归方式把数组或字符串 null转换为空''字符串
        public function _unsetNull($arr){
            if($arr !== null){
                if(is_array($arr)){
                    if(!empty($arr)){
                        foreach($arr as $key => $value){
                            if($value === null){
                                $arr[$key] = '';
                            }else{
                                $arr[$key] = $this->_unsetNull($value);      //递归再去执行
                            }
                        }
                    }else{ $arr = ''; }
                }else{
                    if($arr === null){ $arr = ''; }         //注意三个等号
                }
            }else{ $arr = ''; }
            return $arr;
        }
    
    function _json($arr){
        header("Content-type: application/json; charset=utf-8")
        echo json_encode($arr,JSON_UNESCAPED_UNICODE);//中文
        exit()
    }
  • 相关阅读:
    Postman post csrf_token
    CBV
    nginx+uWSGI+django部署web服务器
    JS绘图
    get_字段_display()
    limit_choices_to
    window.onload=function(){};
    模拟百度搜索项目
    事件冒泡
    解绑事件
  • 原文地址:https://www.cnblogs.com/xuzhengzong/p/8606381.html
Copyright © 2011-2022 走看看