Array for example
$array = array(
array('first'=>5), array('first'=>4), array('second'=>3)
);
How sum values by keys for result:
$result = array(
'first'=>9,
'second'=>3
);
Thanks in advance.
Array for example
$array = array(
array('first'=>5), array('first'=>4), array('second'=>3)
);
How sum values by keys for result:
$result = array(
'first'=>9,
'second'=>3
);
Thanks in advance.
you can iterate array $array and do whatever you want. And you can do it with any language if you have some pprogramming skills.
$result=array();
foreach ($array as $sub) {
foreach ($sub as $key => $value) {
if (isset($result[$key])) $result[$key] += $value;
else $result[$key]=$value;
}
}
Note: if you getting this info from database, it is better to sum it using database resources.
You can use array_walk_recursive:
$results = array();
array_walk_recursive($array, function($number, $key){
global $results;
if (! isset($results[$key])) $results[$key] = 0;
$results[$key] += $number;
});
This works in php >= 5.3
Col. Shrapnel, yes it's work, but result
foreach ($count_groups as $sub) {
foreach ($sub as $key => $value) {
echo $key." ";
if (isset($result[$key])) $result[$key] += $value;
else $result[$key]=$value;
}
echo $result[$key]."<br>";
is list
$first - 5
$first - 9
$second - 3
but need final result
$first - 9
$second - 3
THANKS!!!