Rumah > pembangunan bahagian belakang > tutorial php > 二维数组,算法

二维数组,算法

WBOY
Lepaskan: 2016-06-13 12:18:58
asal
899 orang telah melayarinya

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

------解决思路----------------------
<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 />
Salin selepas log masuk


<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 />
Salin selepas log masuk

------解决思路----------------------
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 />}
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan