php 数组相加合并函数 多个数组相同键名项键值相加。
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

最近做的一个项目要实现多个相同数组的相加合并功能,需要实现对应键名的键值相加,具体说明如下:
有如下多个数组:
array(13) { [14] => string(1) "4" [15] => string(1) "4" [16] => string(1) "4" [17] => string(1) "4" [18] => string(1) "4" [19] => string(2) "30" [20] => string(2) "10" [21] => string(1) "5" [22] => string(1) "2" [23] => string(1) "2" [24] => string(1) "1" [25] => string(2) "17" [26] => string(2) "10" } array(13) { [14] => string(1) "4" [15] => string(1) "4" [16] => string(1) "3" [17] => string(1) "3" [18] => string(1) "4" [19] => string(2) "30" [20] => string(2) "10" [21] => string(1) "5" [22] => string(1) "2" [23] => string(1) "1" [24] => string(1) "1" [25] => string(2) "15" [26] => string(2) "10" } array(13) { [14] => string(1) "3" [15] => string(1) "4" [16] => string(1) "4" [17] => string(1) "4" [18] => string(1) "4" [19] => string(2) "30" [20] => string(1) "5" [21] => string(1) "5" [22] => string(1) "2" [23] => string(1) "2" [24] => string(1) "1" [25] => string(2) "15" [26] => string(2) "10" }要合并为一个数组,其中对应项的值相加,得出的数组如下(以下数组只为示例,并非实际运算结果):
array(13) { [14] => int(11) [15] => int(12) [16] => int(14) [17] => int(14) [18] => int(16) [19] => int(118) [20] => int(34) [21] => int(19) [22] => int(8) [23] => int(7) [24] => int(4) [25] => int(65) [26] => int(38) }具体函数代码:
//数组相同键,值相加。 function array_value_sum(){ $res = array(); foreach (func_get_args() as $arr) { foreach ($arr as $k => $v){ if (!isset($res[$k])){ $res[$k] = $v; }else{ $res[$k] += $v; } } } return $res; }调用方式:
$arr = array(); foreach ($array as $k => $v) { $v = json_decode($v,true); $arr = array_value_sum($v,$arr); } var_dump($arr); 猜你可能想看的VPS
- 补货通知 搬瓦工→CN2 GIA 线路$46.87 年 VPS 补货了 全球[VPS测评]
- 便宜 $2.45 月 512M 内存 14G SSD 1T 流量@1Gb全球[VPS测评]
- Data Ideas→$1.5 月 1GB 内存 10GB SSD 空间虚拟空间(主机)
- XEN 容易超售-HostXen 预热双十一→充 300 送 100 续全球[VPS测评]
- AuroraCloud→180 元 月 1GB 内存 20GB SSD 虚拟空间(主机)
- CloudCone→$15 年 KVM-1GB 30GB 1TB 洛杉矶全球[VPS测评]
- AuroraCloud→240 元 月 1GB 内存 30GB SSD 虚拟空间(主机)
- 适合做站 LOCVPS→美国圣何塞 CN2 线路 VPS 德国 CN2 美国VPS[主机]
- Cenchu→$10 年 不限空间 不限流量 可绑定 3 个域名 DDO虚拟空间(主机)
- stsdust 杭州联通大带宽 nat 预售二期,便宜国内大带宽 nat全球[VPS测评]
- XenSpec→新上洛杉矶机房 首月 6 折 1.77 美元起 KVM 全球[VPS测评]
- 便宜 $7 年 2G 内存 30G SSD 2T 流量@100Mbps 全球[VPS测评]
- 搬瓦工 $29.88 年 2G 内存 40G SSD 1T 流量 KVM全球[VPS测评]
- 如何通过正确整理旧文章来增加网站SEO优化全球[VPS测评]
- Wikihost → 波特兰CERA VPS PDX → 512M 全球[VPS测评]
- 如何设置指定路径跳转指定域名?全球[VPS测评]
- 向日葵-漏洞科普:海外云服务器三种漏洞修复方法快收藏起来!全球[VPS测评]
- 八成企业看好出海投资 防止“溺水”需重视这一点全球[VPS测评]
- UOVZ怎么样?香港30M大带宽VPS 直连线路月付50元香港VPS[主机]
- 中国云计算,能啃的只剩硬骨头了全球[VPS测评]
- Vultr新用户100美金 最新活动来啦!新用户充值活动来了,充100美全球[VPS测评]
- 搬瓦工:美国香港cn2 gia线路VPS,10Gbps带宽,移动+联通企美国VPS[主机]
- 众创逸云:元旦优惠活动,香港美国CN2云服务器4核4G30M,仅售309美国VPS[主机]
- 蓝米云:美国cera vps/香港cn2vps套餐,1核2G/40G/1美国VPS[主机]
- wikihost,微基主机服务,100M带宽香港tudcloud,5G防香港VPS[主机]
- 拼多多入局跨境电商平台,首站美国;奈雪的茶在上海建第二总部;pidan“美国VPS[主机]
- 蓝队云服务器怎么样?蓝队云服务器价格和优势是什么?全球[VPS测评]
- 亿速云服务器双12活动:香港云服务器29元/月起,100GCDN流量只需香港VPS[主机]
- 酷锐云元旦钜惠:大量香港/美国vps优惠码,香港1核2G云主机仅20元/美国VPS[主机]
- Vmshell:香港免费vps试用机24H,新增在线测速!年付再打9折,香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-208449.html











