我的数组是这样的:
array(
(int) 0 => array(
'projet_id' => '1',
'activite_id' => '1',
'domaine_id' => null,
'aretirer' => (float) 4
),
(int) 1 => array(
'projet_id' => '1',
'activite_id' => '3',
'domaine_id' => null,
'aretirer' => (float) 1
),
(int) 2 => array(
'projet_id' => '1',
'activite_id' => '1',
'domaine_id' => null,
'aretirer' => (float) 2
),
(int) 3 => array(
'projet_id' => '1',
'activite_id' => '3',
'domaine_id' => null,
'aretirer' => (float) 2
)
)
我想总结 projet_id、activite_id 和 domaine_id 相同的 aretirer 键
我尝试使用 foreach 但我没有得到这样的好结果
array(
(int) 0 => array(
'projet_id' => '1',
'activite_id' => '1',
'domaine_id' => null,
'aretirer' => (float) 6
),
(int) 1 => array(
'projet_id' => '1',
'activite_id' => '3',
'domaine_id' => null,
'aretirer' => (float) 3
)
)
这就是我试图做的
public function array_sum($array){
$arrayfusion=array();
$i=0;
foreach($array as $item):
$arrayfusion[$i]['projet_id']=$item['projet_id'];
$arrayfusion[$i]['activite_id']=$item['activite_id'];
$arrayfusion[$i]['domaine_id']=$item['domaine_id'];
$arrayfusion[$i]['aretirer']=$item['aretirer'];
foreach($array as $itemnext):
if($itemnext['projet_id']==$item['projet_id'] && $itemnext['activite_id']==$item['activite_id'] && $itemnext['domaine_id']==$item['domaine_id']):
$arrayfusion[$i]['aretirer']+=$itemnext['aretirer'];
endif;
endforeach;
$i++;
endforeach;
return $arrayfusion;
}
感谢您的帮助