Reputation: 2336
I have a tree structure formed by arrays in PHP, these trees have the following structure.
array(5) {
["guid"]=>
string(32) "bfd08465daebc6a624f81fb38fdcb357"
["name"]=>
string(7) "Activos"
["parent_guid"]=>
string(32) "3caed17eb39d20aa9d409f2e61d457ff"
["suma"]=>
NULL
["children"]=>
array(1) {
[0]=>
array(5) {
["guid"]=>
string(32) "c0227d82bf3926d0517a0cffce66be31"
["name"]=>
string(14) "Current Assets"
["parent_guid"]=>
string(32) "bfd08465daebc6a624f81fb38fdcb357"
["suma"]=>
NULL
["children"]=>
array(3) {
[0]=>
array(5) {
["guid"]=>
string(32) "c7bc90c45a5319a00f8a64c7ae1a5ca9"
["name"]=>
string(16) "Cuenta de ahorro"
["parent_guid"]=>
string(32) "c0227d82bf3926d0517a0cffce66be31"
["suma"]=>
string(11) "111035.3800"
["children"]=>
array(0) {
}
}
[1]=>
array(5) {
["guid"]=>
string(32) "72a3ed3467cfd24671197ad7d7f9bb40"
["name"]=>
string(17) "Cuenta de cheques"
["parent_guid"]=>
string(32) "c0227d82bf3926d0517a0cffce66be31"
["suma"]=>
string(6) "0.0000"
["children"]=>
array(0) {
}
}
If a node has children, then the index "children" will have a nested array containing values. The question here is, how do I sum all values in index "suma" for every children given a "guid"?
For example, given guid c7bc90c45a5319a00f8a64c7ae1a5ca9 the function must returns 111035.3800 since this subtree has no children. But with c0227d82bf3926d0517a0cffce66be31 the function must sum Null + 111035.3800 + 0.0000 which are the values in index "suma" for the given guid and its repective children. If a node has 'n' nested children then the function should iterate through all child and sum the value in index "suma".
I've already have a recursive function which returns a subtree given an guid value but I don't know how to work the sum.
function getChildrenAccounts($array, $parent_guid)
{
if(!is_array($array))
return null;
if(isset($array['guid']) && $array['guid'] == $parent_guid)
return $array['children'];
foreach ($array as $item) {
$return = $this->getChildrenAccounts($item, $parent_guid);
if (!is_null($return))
return $return;
}
return null;
}
Any help would be appreciated, thanks
Upvotes: 2
Views: 1804
Reputation: 22820
Code :
function getChildrenSum($array)
{
$sum = 0;
if (count($array)>0)
{
foreach ($array as $item)
{
$sum += $item['suma'];
$sum += getChildrenSum($item['children']);
}
}
else return 0;
}
function getSumFromArray($array,$guid)
{
foreach ($array as $item)
{
if (isset($item['guid']))
if ($item['guid']==$guid)
return getChildrenSum($item['children']);
}
return 0;
}
Usage :
$total = getSumFromArray($array,"c0227d82bf3926d0517a0cffce66be31");
Upvotes: 0
Reputation: 21269
Your problem is in your function definition. You have not decided what your function will do.
//What is the return value here?
function getChildrenAccounts($array, $parent_guid) {
If the answer to that is 'many things' then you have a problem. In your case you have 'null', an array, and probably a value (sum of the 'suma' field).
I recommend writing a function:
function sumChildrenAccounts($array, $parent_guid, &$sum) {
if(!is_array($array)) { return null; }//this needs to be caught as an error!
if(isset($array['guid']) && $array['guid'] == $parent_guid) { //a valid array
foreach ($array['children'] as $child) {
$sum = $this->sumChildrenAccounts($child, $parent_guid, $sum) + $array['suma'];
}
return $sum;
}
return null;//also an error!!!
}
Upvotes: 2