开发者

Combining arrays with calculation (sum)

I have these two arrays result in print_r:

Array ( [0] => multidesign1 [1] => multidesign1 [2] => multidesign2 [3] => multidesign2 )    
Array ( [0] => 30 [1] => 7 [2] => 40 [3] => 1 ) 
开发者_开发百科

The actual contents should be:

multidesign1 has 30 and 7 so its 37 while multidesign2 has 40 and 1 so its 41.

Can I combine these two arrays so I could come up with multidesign1=>37 and multidesign2=>41 ?

Thanks.


$newArray = array();
foreach(range(0, count($firstArray) - 1) as $index) {
    if(isset($newArray[$index])) {
        $newArray[$index] += $secondArray[$index];
    } else {
        $newArray[$index] = $secondArray[$index];
    }
}

Is that something like what you're looking for?


$result = array();
foreach ($array1 as $i => $key) {
    $result[$key] += $array2[$i];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜