Home > Backend Development > PHP Tutorial > 二维数组,算法

二维数组,算法

WBOY
Release: 2016-06-13 12:18:58
Original
899 people have browsed it

二维数组,求一个算法
这个数组是这样子的,怎么把这样的数组,相同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));
Copy after login
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 />
Copy after login

------解决思路----------------------
<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 />
Copy after login


<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 />
Copy after login

------解决思路----------------------
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 />}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template