Sums all columns in multidimensional array

Uses array_walk_recursive() to sum all rows by keys in array and returns array with sums. Read more.

php
fork add to bundle source
function summarize_array( $array ) { $totals = []; array_walk_recursive($array, function($item, $key) use (&$totals){ if ( is_numeric($item) ) { $totals[$key] += $item; } }); return $totals; }

usage

load_function('summarize_array'); $array = [ ['pounds' => 10, 'size' => 5], ['pounds' => 2, 'size' => 2] ]; $res = summarize_array($array); print_r($res); // Array // ( // [pounds] => 12 // [size] => 7 // )
← how to use this in app?
create log ...inout