How to sum all column values in multi-dimensional array?

后端 未结 20 2552
花落未央
花落未央 2020-11-22 00:57

How can I add all the columnar values by associative key? Note that key sets are dynamic.

Input array:

Arr         


        
20条回答
  •  一整个雨季
    2020-11-22 01:20

    this works great on my laravel project

    print_r($Array); // your original array
    
    $_SUM = [];
    
    // count($Array[0]) => if the number of keys are equall in all arrays then do a count of index 0 etc.
    for ($i=0; $i < count($Array[0]); $i++) {
        $_SUM[] = $Array[0][$i] + $Array[1][$i]; // do a for loop on the count 
    }
    
    print_r($_SUM); // get a sumed up array
    

提交回复
热议问题