二次元配列、アルゴリズムを見つけます
この配列は次のようになります。このような配列で同じ従業員 ID を持つイェジを加算する方法
Array
(
[0] => Array
(
[従業員 ID] => 26
[yeji] => 60
)
[1] => 配列
(
[従業員 ID] => 25
[イェジ] => 105
)
[2] => 配列
(
[従業員 ID] => 26
yeji] = > 100
)
[3] => 配列
(
[従業員 ID] => 25
[yeji] => 0
)
)
マージされた配列は次のようになります
Array
(
[0] => Array
(
[employee_id] => 26 [イェジ] => 105
)
)
-----解決策のアイデア----------- ----------
-----ソリューションアイデア-------------- --------
$ar = array (<br /> 0 => array (<br /> 'employee_id' => '26',<br /> 'yeji' => '60',<br /> ),<br /> 1 => array (<br /> 'employee_id' => '25',<br /> 'yeji' => '105',<br /> ),<br /> 2 => array (<br /> 'employee_id' => '26',<br /> 'yeji' => '100',<br /> ),<br /> 3 => array (<br /> 'employee_id' => '25',<br /> 'yeji' => '0',<br /> ),<br />);<br /><br />$res = array();<br />foreach($ar as $item) {<br /> $res[$item['employee_id']]['employee_id'] = $item['employee_id'];<br /> @$res[$item['employee_id']]['yeji'] += $item['yeji'];<br />}<br />print_r(array_values($res));
Array<br />(<br /> [0] => Array<br /> (<br /> [employee_id] => 26<br /> [yeji] => 160<br /> )<br /><br /> [1] => Array<br /> (<br /> [employee_id] => 25<br /> [yeji] => 105<br /> )<br /><br />)<br /><br />
<br />$arr = array (<br /> 0 => array (<br /> 'employee_id' => '26',<br /> 'yeji' => '60',<br /> ),<br /> 1 => array (<br /> 'employee_id' => '25',<br /> 'yeji' => '105',<br /> ),<br /> 2 => array (<br /> 'employee_id' => '26',<br /> 'yeji' => '100',<br /> ),<br /> 3 => array (<br /> 'employee_id' => '25',<br /> 'yeji' => '0',<br /> ),<br />);<br />$item=array();<br />foreach($arr as $k=>$v){<br /> if(!isset($item[$v['employee_id']])){<br /> $item[$v['employee_id']]['employee_id']=$v['employee_id'];<br /> $item[$v['employee_id']]['yeji']=$v['yeji'];<br /> }else{<br /> $item[$v['employee_id']]['yeji']+=$v['yeji'];<br /> }<br />}<br />print_r($item);<br />
<br />Array<br />(<br /> [26] => Array<br /> (<br /> [employee_id] => 26<br /> [yeji] => 160<br /> )<br /><br /> [25] => Array<br /> (<br /> [employee_id] => 25<br /> [yeji] => 105<br /> )<br /><br />)<br /><br />