簡體   English   中英

使用第一個數組作為鍵組合兩個數組(但有重復項),然后添加具有匹配重復項的第二個數組值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM