情景:对于以下从数据库中获取的数据原始数据来说,我们要对这个数组进行一些翻译,1,2,3,4这样的数字对于前台用户来说是不友好的,我们将其翻译成中文
当然,解决方案有以下三种
一:简单粗暴,利用模版引擎去进行输出,在输出的时候进行相应的改变,这在各种模版引擎中使用非常普遍,就不用介绍了
二:利用前端的js进行转换,这对于单条数据或者说详情页来说,是比较容易的,但是遇到大量数据的列表展示时,js使用起来就不叫笨重,不推荐使用
三:使用后台php装换(二维数组)
[list] => Array ( [0] => Array ( [status] => 已报名 [add_time] => 2016-11-11 16:19:41 [field_142] => efsohi@jd.omc [field_143] => [user_id] => 312398 [is_register] => 1 [rank_num] => 1 [deal] => 1 [form_id] => 2 ) [1] => Array ( [status] => 已报名 [add_time] => 2016-11-11 16:18:40 [field_142] => 1098325951@qq.com [field_143] => [user_id] => 257216 [is_register] => 0 [rank_num] => 1 [deal] => 1 [form_id] => 1 ) )
转换代码如下:
//对从数据库中返回的数组进行翻译 $dealField = array(1=>"<span style='color:red'>未处理</span>" , 2=>"<span style='color:green'>已处理</span>"); $infoArr = $info['list']; if($infoArr){ foreach ($infoArr as $vv_key => $vv_value) { $infoArr[$vv_key]['is_register'] = ($vv_value['is_register'] == 1 ) ?'是' : '否' ; $infoArr[$vv_key]['deal'] = isset($dealField[$vv_value['deal']]) ? $dealField[$vv_value['deal']] : '未知情况' ; } $info['list'] = $infoArr; }
再来一个一维数组的翻译
数组如下:
Array ( [acitvity_info_id] => 1601 [status] => 已报名 [add_time] => 2016-11-11 16:18:40 [source] => pc [child_flag] => yudssdf [field_141] => 13296691544 [field_142] => 1098325951@qq.com [field_143] => [field_144] => 处理装备 [user_id] => 257216 [is_register] => 0 [sn_num] => 14788523204952 [client_ip] => 127.0.0.1 [rank_num] => 1 [deal] => 1 [form_id] => 1 )
进行如下翻译
$dealField = array(1=>"<span style='color:red'>未处理</span>" , 2=>"<span style='color:green'>已处理</span>"); if($retValue){ $retValue['is_register'] = ($retValue['is_register'] == 1 ) ?'是' : '否' ; $retValue['deal'] = isset($dealField[$retValue['deal']]) ? $dealField[$retValue['deal']] : '未知情况' ; }
翻译结果如下
Array ( [acitvity_info_id] => 1601 [status] => 已报名 [add_time] => 2016-11-11 16:18:40 [source] => pc [child_flag] => yudssdf [field_141] => 13296691544 [field_142] => 1098325951@qq.com [field_143] => [field_144] => 处理装备 [user_id] => 257216 [is_register] => 否 [sn_num] => 14788523204952 [client_ip] => 127.0.0.1 [rank_num] => 1 [deal] => 未处理 [form_id] => 1 )