The array is formed as follows:

$arr[] = [1,2,3] $arr[] = [2,3,5] $arr[] = [1,1,1] 

At the output you need to get one array: [4,6,9]

How to make the most beautiful?

  • and based on what kind of logic should such an outgoing array be obtained? - Alexey Shimansky
  • one
    @ AlekseyShimansky sum of Nth elements of subarrays (sum up vertically) :) - Zhukov Roman
  • @ php-mvc Use a decorative font to make it as beautiful as possible :) - Vlad from Moscow
  • @ZhukovRoman mmm .. clear. It is a pity that the vehicle could not explain it - Alexey Shimansky
  • @ php-mvc finally need a solution for comparison, maybe we can’t write more beautifully - Zhukov Roman

1 answer 1

 foreach ($arr as $value1lvl) { foreach ($value1lvl as $key2lvl=>$value2lvl) { if (!isset($sum[$key2lvl])) {$sum[$key2lvl]=0;} $sum[$key2lvl]=$sum[$key2lvl]+$value2lvl; } } 

Demo - http://sandbox.onlinephpfunctions.com/code/a27992153fdfc46fc4b36242b619347f25a9b768