[英]Combining two arrays using the first array as key(but has duplicates) then adding the second array values that has the matching duplicates
我想結合兩個數組。 首先使用第一個數組作為鍵(合並重復項),然后將第二個數組中的值相加以適應特定的鍵
//first array
array('1','0','1');
//second array
array('50','10','20');
//output -> first array ('1','0') second array -> ('70','10')
刪除第一個數組中的重復項,並在第二個數組值中添加相應的“ duplicate”鍵
使用結果數組來收集如下結果:
//first array
$k = array('1','0','1');
//second array
$v = array('50','10','20');
$result = array();
foreach($k as $index => $value) {
if(!isset($result[$value])) {
$result[$value] = 0;
}
$result[$value] += $v[$index];
}
print_r($result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.