Home  >  Article  >  Backend Development  >  php recursive formatting number type

php recursive formatting number type

巴扎黑
巴扎黑Original
2016-12-07 14:14:561113browse

    /**
     * 数字转日期递归
     *@param list
     *@return list
     **/
    function day2str($key,$obj,$keys=array('create_time'=>'Y-m-d','s_time'=>'Y-m-d','e_time'=>'Y-m-d','usetime'=>'Y-m-d G:i:s')){
        if(is_array($obj)){//如果是 obj 或 数组 
            foreach ($obj as $k => $v) {
               $obj[$k] = day2str($k,$v,$keys);//递归 
            }
            return $obj;
        }else{
            if(!$type=  $keys[$key] ){//去掉列表没有的项目
                return  $obj; 
            }
            if(is_numeric($key) ){//去掉数组类型
                return $obj;
            }
            if(!$obj){//去掉空值
                return "";
            }
            if(!is_numeric($obj) ){//去掉已转型过的
                return $obj;
            }
            return date($type,$obj);//数字转字串
        }

Use:

day2str('',$data);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn