php输出数据字段类型自动转换函数

<?php 
//配置api输出格式
function hst_restful_api_filter($datas){
$API_fields = array('id','uid','pid','orders','errorcode','states','gender','lon','lat','distance','integral',
		'bid','ifreply','praise','clicks','pages','marketprice','saleprice','nums','savenums','allowsale','onsaleid',
		'verify','isShop','isOfficial','payonline','created','starttime','endtime','status','leftnums','price','ifRead',
		'bookId','toUid','replies','views','total');


$API_filter_arr = array();
$API_filter_arr['id'] = 'intval';
$API_filter_arr['uid'] = 'intval';
$API_filter_arr['pid'] = 'intval';
$API_filter_arr['orders'] = 'intval';
$API_filter_arr['errorcode'] = 'intval';
$API_filter_arr['states'] = 'intval';
$API_filter_arr['gender'] = 'intval';
$API_filter_arr['lon'] = 'floatval';
$API_filter_arr['lat'] = 'floatval';
$API_filter_arr['distance'] = 'floatval';
$API_filter_arr['integral'] = 'intval';
$API_filter_arr['bid'] = 'intval';
$API_filter_arr['ifreply'] = 'intval';
$API_filter_arr['praise'] = 'intval';
$API_filter_arr['clicks'] = 'intval';
$API_filter_arr['pages'] = 'intval';
$API_filter_arr['marketprice'] = 'floatval';
$API_filter_arr['saleprice'] = 'floatval';
$API_filter_arr['nums'] = 'intval';
$API_filter_arr['savenums'] = 'intval';
$API_filter_arr['allowsale'] = 'intval';
$API_filter_arr['onsaleid'] = 'intval';
$API_filter_arr['verify'] = 'intval';
$API_filter_arr['isShop'] = 'intval';
$API_filter_arr['isOfficial'] = 'intval';
$API_filter_arr['payonline'] = 'intval';
$API_filter_arr['created'] = 'intval';
$API_filter_arr['starttime'] = 'intval';
$API_filter_arr['endtime'] = 'intval';
$API_filter_arr['status'] = 'intval';
$API_filter_arr['leftnums'] = 'intval';
$API_filter_arr['price'] = 'floatval';
$API_filter_arr['ifRead'] = 'intval';
$API_filter_arr['bookId'] = 'intval';
$API_filter_arr['toUid'] = 'intval';
$API_filter_arr['replies'] = 'intval';
$API_filter_arr['views'] = 'intval';
$API_filter_arr['total'] = 'intval';
foreach($datas as $k=>$v){
		if(is_array($v)){
			$datas[$k] = hst_restful_api_filter($datas[$k]);				
		}else{
			if(in_array($k, $API_fields)){
				$datas[$k] = $API_filter_arr[$k]($datas[$k]);				
			}
		}
		
	}
	
	return $datas;
}
?>


你可能感兴趣的:(php输出数据字段类型自动转换函数)