Home > Backend Development > PHP Tutorial > 关于数组的问题。

关于数组的问题。

WBOY
Release: 2016-06-23 14:24:50
Original
921 people have browsed it

我想实现下面的效果

 $result = array(    		100001=>array(    			'proc_id'=>102,	    		'num'=>2,    		),    		100002=>array(    			'proc_id'=>100,	    		'num'=>3,    		)    	);$new = array(   //新数组      100001=>array(              'proc_id'=>102,	      'num'=>2'       )      );$new2 = array(   //新数组2      100003=>array(              'proc_id'=>103,	      'num'=>2'       )      );//想实现的最终结果$result = array(    		100001=>array(    			'proc_id'=>102,	    		'num'=>4,    		),    		100002=>array(    			'proc_id'=>100,	    		'num'=>3,    		),              100003=>array(    			'proc_id'=>103,	    		'num'=>3,    		)    	);不知道说的清不清楚?
Copy after login


回复讨论(解决方案)

array_merge

$ar=array_combine(array_merge(array_keys($new),array_keys($new2)),array_merge($new,$new2));foreach($ar as $k=>$v){    if(!$result[$k]){	     $result[$k] = $v;	}else{	    $result[$k]['num'] += $v['num'];	}}print_r($result);
Copy after login

1楼的方法如果是字符串的键值是可以的,但是对数字键值的行不通的,楼主可以用我下面这个方法
$arr = $result+$new+$new2;
print_r($arr);

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