首頁 > 後端開發 > php教程 > 二维数组,算法

二维数组,算法

WBOY
發布: 2016-06-13 12:18:58
原創
886 人瀏覽過

二维数组,求一个算法
这个数组是这样子的,怎么把这样的数组,相同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 (<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 />
登入後複製

------解决思路----------------------
function calcArrSameId($arr=array())<br />{<br />	foreach ($arr as $key => $value) {<br />		//print_r($value);die;<br />		if(!isset($tmp[$value['employee_id']]))<br />		{<br />			$tmp[$value['employee_id']]['employee_id'] = $value['employee_id'];<br />			$tmp[$value['employee_id']]['yeji'] = $value['yeji'];<br />		}else{<br />			$tmp[$value['employee_id']]['yeji'] += $value['yeji'];<br />		}<br />		<br />	}<br />	$list = array_values($tmp);<br />	print_r($list);exit;<br />}
登入後複製

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板