王小,   "Qty" => 2)arr2 = array(   "Qty" => 4,   "Memo" => 说明)"/> 王小,   "Qty" => 2)arr2 = array(   "Qty" => 4,   "Memo" => 说明)">
Home > Backend Development > PHP Tutorial > 关于数组合并,请高手帮忙解决思路

关于数组合并,请高手帮忙解决思路

WBOY
Release: 2016-06-13 13:50:10
Original
727 people have browsed it

关于数组合并,请高手帮忙
我有两个数组如:
arr1 = array(
  "name" => 王小,
  "Qty" => 2
)

arr2 = array(
  "Qty" => 4,
  "Memo" => 说明
)

上面两个数组合并后达到下面的结果:
arr3 = array(
  "name" => 王小,
  "Qty" => 6,
  "Memo" => 说明
)
让Qty相加。PHP有没有直的函数实现呢?我用array_merge这个不行,会把原来的Qty替换掉。

------解决方案--------------------
$arr=array_merge_recursive($arr1,$arr2);

$arr:Array
(
[name] => 王小
[Qty] => Array
(
[0] => 2
[1] => 4
)

[Memo] => 说明
)

但是这个也不是你要的效果,只好自己写个函数了
------解决方案--------------------
只能自己写函数
------解决方案--------------------
$arr=array_merge_recursive($arr1,$arr2);
$arr['Qty'] = array_sum($arr['Qty']);
------解决方案--------------------
没有直接函数实现

不过你用foreach,也不过就是几行的事,写成函数就行了,你还需要什么?

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