note 104222 deleted from function.array-sum by levim
| From: | levim@php.net | Date: | Tue, 18 Mar 2014 19:41:17 +0000 |
| Subject: | note 104222 deleted from function.array-sum by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198962@lists.php.net to get a copy of this message | ||
Note Submitter: pligor at facebook dot com
----
There was a case where I had a complex array, multi-dimesional and non-symmetric (not all elements
of a sub-array were arrays themselves)
Although all the leaf elements of this array were numbers. In case you have two variables like that
and you wanted to add all the values of the right variable to the left then you would need a
function like below.
This is a recursive function which adds the values of two multidimensional arrays with the same key
structure:
<?php
function multiDimArrayAdd(& $left, $right) //created by George Pligor
{
if(is_array($left) && is_array($right))
{
foreach($left as $key => $val)
{
if( is_array($val) )
{
multiDimArrayAdd($left[$key], $right[$key]);
}
$left[$key] += $right[$key];
}
}
}
?>