联表查询
两个返回值的处理方法:
public static function getUserConsume($period,$character){
$uid=self::getByCharacter($character);
if($uid&&$period){
$userconsumescore = self :: getuserconsumescore($period, $uid);
$userscore = self :: getuserscore($period, $uid);
---------上面返回两个键名相同的二维数组,但是返回字段不同
$keys = array_unique (array_merge(array_keys($userconsumescore), array_keys($userscore)));
//array_keys()获得数组的键名,array_merge()合并数组;如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的)。如果数组是数字索引的,则键名会以连续方式重新索引。
array_unique() 函数移除数组中的重复的值,并返回结果数组。当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除。返回的数组中键名不变
sort($keys);//获得键名的数组
$outdates = array();
$showtotalconsumeaddgx = $showtotalconsumeaddhonor = $showtotaladdhonor = $showtotaladdgx = 0;
// print_r($userconsumescore);
//print_r($keys);
//foreach 循环日期
foreach($keys as $date){
//echo $date . "/n<br />";
$totaladdhonor = isset($userscore[$date]['totaladdhonor']) ? $userscore[$date]['totaladdhonor'] : 0;
$showtotaladdhonor += $totaladdhonor;
$totaladdgx = isset($userscore[$date]['totaladdgx']) ? $userscore[$date]['totaladdgx'] : 0;
$showtotaladdgx += $totaladdgx;
$totalconsumeaddgx = isset($userconsumescore[$date]['addgx']) ? $userconsumescore[$date]['addgx'] : 0;
$showtotalconsumeaddgx += $totalconsumeaddgx;
$totalconsumeaddhonor = isset($userconsumescore[$date]['addhonor']) ? $userconsumescore[$date]['addhonor'] : 0;
$showtotalconsumeaddhonor += $totalconsumeaddhonor;
//方便添加字段,和查阅
$outdates[$date] = array(
'time'=>$date,
'totaladdhonor' => $totaladdhonor,
'showtotaladdhonor' => $showtotaladdhonor,
'totaladdgx' => $totaladdgx,
'showtotaladdgx' => $showtotaladdgx,
'totalconsumeaddgx' => $totalconsumeaddgx,
'showtotalconsumeaddgx' => $showtotalconsumeaddgx,
'totalconsumeaddhonor' => $totalconsumeaddhonor,
'showtotalconsumeaddhonor' => $showtotalconsumeaddhonor );
}
}
return $outdates;
}
常规的方法:
public static function getUserConsume($period,$character){
$uid=self::getByCharacter($character);
if($uid&&$period){
$userconsumescore = self :: getuserconsumescore($period, $uid);
$userscore = self :: getuserscore($period, $uid);
$keys = array_unique (array_merge(array_keys($userconsumescore), array_keys($userscore)));
sort($keys);
foreach($keys as $date){
$return[$date]['time']=$userconsumescore[$date]['time'];
$return[$date]['totaladdhonor']=$userscore[$date]['totaladdhonor'];
$return[$date]['showtotaladdhonor'] +=$userscore[$date]['totaladdhonor'];
$return[$date]['totaladdgx']=$userscore[$date]['totaladdgx'];
$return[$date]['showtotaladdgx'] +=$userscore[$date]['totaladdgx'];
$return[$date]['totalconsumeaddgx'] =$userconsumescore[$date]['addgx'];
$return[$date]['showtotalconsumeaddgx'] +=$userconsumescore[$date]['addgx'];
$return[$date]['totalconsumeaddhonor'] =$userconsumescore[$date]['addhonor'];
$return[$date]['showtotalconsumeaddhonor'] +=$userconsumescore[$date]['addhonor'];
}
return $return;
}
}