时间:2021-07-01 10:21:17 帮助过:3人阅读
$ar = array (
0 => array (
'employee_id' => '26',
'yeji' => '60',
),
1 => array (
'employee_id' => '25',
'yeji' => '105',
),
2 => array (
'employee_id' => '26',
'yeji' => '100',
),
3 => array (
'employee_id' => '25',
'yeji' => '0',
),
);
$res = array();
foreach($ar as $item) {
$res[$item['employee_id']]['employee_id'] = $item['employee_id'];
@$res[$item['employee_id']]['yeji'] += $item['yeji'];
}
print_r(array_values($res));
Array
(
[0] => Array
(
[employee_id] => 26
[yeji] => 160
)
[1] => Array
(
[employee_id] => 25
[yeji] => 105
)
)
$arr = array (
0 => array (
'employee_id' => '26',
'yeji' => '60',
),
1 => array (
'employee_id' => '25',
'yeji' => '105',
),
2 => array (
'employee_id' => '26',
'yeji' => '100',
),
3 => array (
'employee_id' => '25',
'yeji' => '0',
),
);
$item=array();
foreach($arr as $k=>$v){
if(!isset($item[$v['employee_id']])){
$item[$v['employee_id']]['employee_id']=$v['employee_id'];
$item[$v['employee_id']]['yeji']=$v['yeji'];
}else{
$item[$v['employee_id']]['yeji']+=$v['yeji'];
}
}
print_r($item);
Array
(
[26] => Array
(
[employee_id] => 26
[yeji] => 160
)
[25] => Array
(
[employee_id] => 25
[yeji] => 105
)
)
function calcArrSameId($arr=array())
{
foreach ($arr as $key => $value) {
//print_r($value);die;
if(!isset($tmp[$value['employee_id']]))
{
$tmp[$value['employee_id']]['employee_id'] = $value['employee_id'];
$tmp[$value['employee_id']]['yeji'] = $value['yeji'];
}else{
$tmp[$value['employee_id']]['yeji'] += $value['yeji'];
}
}
$list = array_values($tmp);
print_r($list);exit;
}