这个数组是这样子的,怎么把这样的数组,相同employee_id的yeji加起来
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
)
)
合并后的数组是这样
Array
(
[0] => Array
(
[employee_id] => 26
[yeji] => 160
)
[1] => Array
(
[employee_id] => 25
[yeji] => 105
)
)
------解决思路----------------------
$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;
}