How to use recursion to sum specific values in a multidimensional array?
up vote
-4
down vote
favorite
I have a multi-level object data with a structure similar to the window below.
I am trying to
- sum all the direct package values for the top users or say
parents
, only - sum all the nested indirect package values from the
children
tree.
The final result is expected to be similar to:["sum_direct": 600,"sum_indirect": 3000]
I need help to know what I'm not doing right:
function sumDirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
$sum += $package['package']['direct'];
else
$sum = 0;
return $sum;
function sumIndirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
if(count($package['children']) > 0)
foreach ($package['children'] as $children)
$sum += $children['indirect'];
$sum += sumIndirect($children);
else
$sum = 0;
return $sum;
Sample input:
"users": [
"user_id": 2,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 58,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 59,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 111,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 116,
"ref_id": 2,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 119,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
,
"user_id": 100,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 101,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 102,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 103,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 104,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 105,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 106,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 107,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 108,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 109,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 110,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 117,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 129,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 130,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
]
Complete data at users.json
php arrays json recursion multidimensional-array
add a comment |
up vote
-4
down vote
favorite
I have a multi-level object data with a structure similar to the window below.
I am trying to
- sum all the direct package values for the top users or say
parents
, only - sum all the nested indirect package values from the
children
tree.
The final result is expected to be similar to:["sum_direct": 600,"sum_indirect": 3000]
I need help to know what I'm not doing right:
function sumDirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
$sum += $package['package']['direct'];
else
$sum = 0;
return $sum;
function sumIndirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
if(count($package['children']) > 0)
foreach ($package['children'] as $children)
$sum += $children['indirect'];
$sum += sumIndirect($children);
else
$sum = 0;
return $sum;
Sample input:
"users": [
"user_id": 2,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 58,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 59,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 111,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 116,
"ref_id": 2,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 119,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
,
"user_id": 100,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 101,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 102,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 103,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 104,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 105,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 106,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 107,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 108,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 109,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 110,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 117,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 129,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 130,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
]
Complete data at users.json
php arrays json recursion multidimensional-array
So what is the question? Read How to Ask and create a Minimal, Complete, and Verifiable example
– Alon Eitan
Nov 10 at 7:13
@Bishoplee When a question is Unclear or there is no evidence of effort, you will find that the question gets voted down. Please improve your question if you would like to prevent downvotes and run the chance of receiving upvotes.
– mickmackusa
Nov 10 at 22:35
add a comment |
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
I have a multi-level object data with a structure similar to the window below.
I am trying to
- sum all the direct package values for the top users or say
parents
, only - sum all the nested indirect package values from the
children
tree.
The final result is expected to be similar to:["sum_direct": 600,"sum_indirect": 3000]
I need help to know what I'm not doing right:
function sumDirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
$sum += $package['package']['direct'];
else
$sum = 0;
return $sum;
function sumIndirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
if(count($package['children']) > 0)
foreach ($package['children'] as $children)
$sum += $children['indirect'];
$sum += sumIndirect($children);
else
$sum = 0;
return $sum;
Sample input:
"users": [
"user_id": 2,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 58,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 59,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 111,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 116,
"ref_id": 2,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 119,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
,
"user_id": 100,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 101,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 102,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 103,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 104,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 105,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 106,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 107,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 108,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 109,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 110,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 117,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 129,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 130,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
]
Complete data at users.json
php arrays json recursion multidimensional-array
I have a multi-level object data with a structure similar to the window below.
I am trying to
- sum all the direct package values for the top users or say
parents
, only - sum all the nested indirect package values from the
children
tree.
The final result is expected to be similar to:["sum_direct": 600,"sum_indirect": 3000]
I need help to know what I'm not doing right:
function sumDirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
$sum += $package['package']['direct'];
else
$sum = 0;
return $sum;
function sumIndirect($c)
if(count($c) > 0)
$sum = 0;
foreach ($c as $package)
if(count($package['children']) > 0)
foreach ($package['children'] as $children)
$sum += $children['indirect'];
$sum += sumIndirect($children);
else
$sum = 0;
return $sum;
Sample input:
"users": [
"user_id": 2,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 58,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 59,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 111,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 116,
"ref_id": 2,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 119,
"ref_id": 2,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
,
"user_id": 100,
"ref_id": 1,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": [
"user_id": 101,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 102,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 103,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 104,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 105,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 106,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 107,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 108,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 109,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 110,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
,
"user_id": 117,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 129,
"ref_id": 100,
"package": [
"name": "Diamond"
,
"direct": 1000
,
"indirect": 500
],
"children": 0
,
"user_id": 130,
"ref_id": 100,
"package": [
"name": "Basic"
,
"direct": 200
,
"indirect": 100
],
"children": 0
]
]
Complete data at users.json
php arrays json recursion multidimensional-array
php arrays json recursion multidimensional-array
edited Nov 10 at 22:49
mickmackusa
21.4k83256
21.4k83256
asked Nov 10 at 7:08
Bishoplee
1210
1210
So what is the question? Read How to Ask and create a Minimal, Complete, and Verifiable example
– Alon Eitan
Nov 10 at 7:13
@Bishoplee When a question is Unclear or there is no evidence of effort, you will find that the question gets voted down. Please improve your question if you would like to prevent downvotes and run the chance of receiving upvotes.
– mickmackusa
Nov 10 at 22:35
add a comment |
So what is the question? Read How to Ask and create a Minimal, Complete, and Verifiable example
– Alon Eitan
Nov 10 at 7:13
@Bishoplee When a question is Unclear or there is no evidence of effort, you will find that the question gets voted down. Please improve your question if you would like to prevent downvotes and run the chance of receiving upvotes.
– mickmackusa
Nov 10 at 22:35
So what is the question? Read How to Ask and create a Minimal, Complete, and Verifiable example
– Alon Eitan
Nov 10 at 7:13
So what is the question? Read How to Ask and create a Minimal, Complete, and Verifiable example
– Alon Eitan
Nov 10 at 7:13
@Bishoplee When a question is Unclear or there is no evidence of effort, you will find that the question gets voted down. Please improve your question if you would like to prevent downvotes and run the chance of receiving upvotes.
– mickmackusa
Nov 10 at 22:35
@Bishoplee When a question is Unclear or there is no evidence of effort, you will find that the question gets voted down. Please improve your question if you would like to prevent downvotes and run the chance of receiving upvotes.
– mickmackusa
Nov 10 at 22:35
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Recursion doesn't seem to be necessary for the "parent" direct values, so that can be done with a direct foreach loop.
The "children" indirect values require recursion. I've elected to use array_walk_recursive()
to access every "leaf node" in the array. This will also store the parents' indirect values too, so I subtract those values after array_walk_recursive()
is finished.
Code: (Demo)
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$parents_indirect = 0;
echo "<pre>";
$array = json_decode($json, true)['users'];
foreach ($array as $users)
$result['sum_direct'] += $users['package'][1]['direct'];
$parents_indirect += $users['package'][2]['indirect'];
array_walk_recursive($array, function ($v, $k) use (&$result)
if ($k === 'indirect')
$result['sum_indirect'] += $v;
);
$result['sum_indirect'] -= $parents_indirect;
echo json_encode($result);
Output using your in-question data:
"sum_direct":400,"sum_indirect":3000
Output using your full json from github:
"sum_direct":600,"sum_indirect":22400
You've been a great source of encouragement. Looking at the demo you created, what can be the likely impact on the server with a large data since this is expected to grow over time?
– Bishoplee
Nov 11 at 21:20
Are you happy with this solution or would you rather have a custom-coded recursive call? Are the results correct? Questions are most-clear when the EXACT expected output is stated in the question.
– mickmackusa
Nov 11 at 21:23
The results are correct though but what do you imply with custom-coded recursive call?
– Bishoplee
Nov 11 at 21:26
I have no way of guessing the size of your data over time. I also haven't benchmarked my solution on the sample data. Once you have a working solution, you can post your code on CodeReview and ask about optimization. If you are truly concerned about performance, my first recommendation is to refine your data structure because it is currently bloated with too many levels.
– mickmackusa
Nov 11 at 21:29
Yoursum_indirect()
is a custom function. I used a native recursive function.
– mickmackusa
Nov 11 at 21:30
|
show 5 more comments
up vote
0
down vote
a simple way could be based on a couple for foreach one nested the other
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$myArray = json_decode($myJsonString, TRUE)
foreach($myArray['users'] as $value)
$result['sum_direct'] += $value['package']['direct'];
foreach($value['children']['package'] as $value2)
$result['sum_indirect'] += $value2['indirect'];
$jsonResult= json_encode($result);
@mickmackusa .. correct .. answer updated
– scaisEdge
Nov 10 at 7:48
@mickmackusa I updated with the attempts made at getting the summations.
– Bishoplee
Nov 10 at 21:24
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Recursion doesn't seem to be necessary for the "parent" direct values, so that can be done with a direct foreach loop.
The "children" indirect values require recursion. I've elected to use array_walk_recursive()
to access every "leaf node" in the array. This will also store the parents' indirect values too, so I subtract those values after array_walk_recursive()
is finished.
Code: (Demo)
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$parents_indirect = 0;
echo "<pre>";
$array = json_decode($json, true)['users'];
foreach ($array as $users)
$result['sum_direct'] += $users['package'][1]['direct'];
$parents_indirect += $users['package'][2]['indirect'];
array_walk_recursive($array, function ($v, $k) use (&$result)
if ($k === 'indirect')
$result['sum_indirect'] += $v;
);
$result['sum_indirect'] -= $parents_indirect;
echo json_encode($result);
Output using your in-question data:
"sum_direct":400,"sum_indirect":3000
Output using your full json from github:
"sum_direct":600,"sum_indirect":22400
You've been a great source of encouragement. Looking at the demo you created, what can be the likely impact on the server with a large data since this is expected to grow over time?
– Bishoplee
Nov 11 at 21:20
Are you happy with this solution or would you rather have a custom-coded recursive call? Are the results correct? Questions are most-clear when the EXACT expected output is stated in the question.
– mickmackusa
Nov 11 at 21:23
The results are correct though but what do you imply with custom-coded recursive call?
– Bishoplee
Nov 11 at 21:26
I have no way of guessing the size of your data over time. I also haven't benchmarked my solution on the sample data. Once you have a working solution, you can post your code on CodeReview and ask about optimization. If you are truly concerned about performance, my first recommendation is to refine your data structure because it is currently bloated with too many levels.
– mickmackusa
Nov 11 at 21:29
Yoursum_indirect()
is a custom function. I used a native recursive function.
– mickmackusa
Nov 11 at 21:30
|
show 5 more comments
up vote
0
down vote
accepted
Recursion doesn't seem to be necessary for the "parent" direct values, so that can be done with a direct foreach loop.
The "children" indirect values require recursion. I've elected to use array_walk_recursive()
to access every "leaf node" in the array. This will also store the parents' indirect values too, so I subtract those values after array_walk_recursive()
is finished.
Code: (Demo)
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$parents_indirect = 0;
echo "<pre>";
$array = json_decode($json, true)['users'];
foreach ($array as $users)
$result['sum_direct'] += $users['package'][1]['direct'];
$parents_indirect += $users['package'][2]['indirect'];
array_walk_recursive($array, function ($v, $k) use (&$result)
if ($k === 'indirect')
$result['sum_indirect'] += $v;
);
$result['sum_indirect'] -= $parents_indirect;
echo json_encode($result);
Output using your in-question data:
"sum_direct":400,"sum_indirect":3000
Output using your full json from github:
"sum_direct":600,"sum_indirect":22400
You've been a great source of encouragement. Looking at the demo you created, what can be the likely impact on the server with a large data since this is expected to grow over time?
– Bishoplee
Nov 11 at 21:20
Are you happy with this solution or would you rather have a custom-coded recursive call? Are the results correct? Questions are most-clear when the EXACT expected output is stated in the question.
– mickmackusa
Nov 11 at 21:23
The results are correct though but what do you imply with custom-coded recursive call?
– Bishoplee
Nov 11 at 21:26
I have no way of guessing the size of your data over time. I also haven't benchmarked my solution on the sample data. Once you have a working solution, you can post your code on CodeReview and ask about optimization. If you are truly concerned about performance, my first recommendation is to refine your data structure because it is currently bloated with too many levels.
– mickmackusa
Nov 11 at 21:29
Yoursum_indirect()
is a custom function. I used a native recursive function.
– mickmackusa
Nov 11 at 21:30
|
show 5 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Recursion doesn't seem to be necessary for the "parent" direct values, so that can be done with a direct foreach loop.
The "children" indirect values require recursion. I've elected to use array_walk_recursive()
to access every "leaf node" in the array. This will also store the parents' indirect values too, so I subtract those values after array_walk_recursive()
is finished.
Code: (Demo)
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$parents_indirect = 0;
echo "<pre>";
$array = json_decode($json, true)['users'];
foreach ($array as $users)
$result['sum_direct'] += $users['package'][1]['direct'];
$parents_indirect += $users['package'][2]['indirect'];
array_walk_recursive($array, function ($v, $k) use (&$result)
if ($k === 'indirect')
$result['sum_indirect'] += $v;
);
$result['sum_indirect'] -= $parents_indirect;
echo json_encode($result);
Output using your in-question data:
"sum_direct":400,"sum_indirect":3000
Output using your full json from github:
"sum_direct":600,"sum_indirect":22400
Recursion doesn't seem to be necessary for the "parent" direct values, so that can be done with a direct foreach loop.
The "children" indirect values require recursion. I've elected to use array_walk_recursive()
to access every "leaf node" in the array. This will also store the parents' indirect values too, so I subtract those values after array_walk_recursive()
is finished.
Code: (Demo)
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$parents_indirect = 0;
echo "<pre>";
$array = json_decode($json, true)['users'];
foreach ($array as $users)
$result['sum_direct'] += $users['package'][1]['direct'];
$parents_indirect += $users['package'][2]['indirect'];
array_walk_recursive($array, function ($v, $k) use (&$result)
if ($k === 'indirect')
$result['sum_indirect'] += $v;
);
$result['sum_indirect'] -= $parents_indirect;
echo json_encode($result);
Output using your in-question data:
"sum_direct":400,"sum_indirect":3000
Output using your full json from github:
"sum_direct":600,"sum_indirect":22400
answered Nov 10 at 22:43
mickmackusa
21.4k83256
21.4k83256
You've been a great source of encouragement. Looking at the demo you created, what can be the likely impact on the server with a large data since this is expected to grow over time?
– Bishoplee
Nov 11 at 21:20
Are you happy with this solution or would you rather have a custom-coded recursive call? Are the results correct? Questions are most-clear when the EXACT expected output is stated in the question.
– mickmackusa
Nov 11 at 21:23
The results are correct though but what do you imply with custom-coded recursive call?
– Bishoplee
Nov 11 at 21:26
I have no way of guessing the size of your data over time. I also haven't benchmarked my solution on the sample data. Once you have a working solution, you can post your code on CodeReview and ask about optimization. If you are truly concerned about performance, my first recommendation is to refine your data structure because it is currently bloated with too many levels.
– mickmackusa
Nov 11 at 21:29
Yoursum_indirect()
is a custom function. I used a native recursive function.
– mickmackusa
Nov 11 at 21:30
|
show 5 more comments
You've been a great source of encouragement. Looking at the demo you created, what can be the likely impact on the server with a large data since this is expected to grow over time?
– Bishoplee
Nov 11 at 21:20
Are you happy with this solution or would you rather have a custom-coded recursive call? Are the results correct? Questions are most-clear when the EXACT expected output is stated in the question.
– mickmackusa
Nov 11 at 21:23
The results are correct though but what do you imply with custom-coded recursive call?
– Bishoplee
Nov 11 at 21:26
I have no way of guessing the size of your data over time. I also haven't benchmarked my solution on the sample data. Once you have a working solution, you can post your code on CodeReview and ask about optimization. If you are truly concerned about performance, my first recommendation is to refine your data structure because it is currently bloated with too many levels.
– mickmackusa
Nov 11 at 21:29
Yoursum_indirect()
is a custom function. I used a native recursive function.
– mickmackusa
Nov 11 at 21:30
You've been a great source of encouragement. Looking at the demo you created, what can be the likely impact on the server with a large data since this is expected to grow over time?
– Bishoplee
Nov 11 at 21:20
You've been a great source of encouragement. Looking at the demo you created, what can be the likely impact on the server with a large data since this is expected to grow over time?
– Bishoplee
Nov 11 at 21:20
Are you happy with this solution or would you rather have a custom-coded recursive call? Are the results correct? Questions are most-clear when the EXACT expected output is stated in the question.
– mickmackusa
Nov 11 at 21:23
Are you happy with this solution or would you rather have a custom-coded recursive call? Are the results correct? Questions are most-clear when the EXACT expected output is stated in the question.
– mickmackusa
Nov 11 at 21:23
The results are correct though but what do you imply with custom-coded recursive call?
– Bishoplee
Nov 11 at 21:26
The results are correct though but what do you imply with custom-coded recursive call?
– Bishoplee
Nov 11 at 21:26
I have no way of guessing the size of your data over time. I also haven't benchmarked my solution on the sample data. Once you have a working solution, you can post your code on CodeReview and ask about optimization. If you are truly concerned about performance, my first recommendation is to refine your data structure because it is currently bloated with too many levels.
– mickmackusa
Nov 11 at 21:29
I have no way of guessing the size of your data over time. I also haven't benchmarked my solution on the sample data. Once you have a working solution, you can post your code on CodeReview and ask about optimization. If you are truly concerned about performance, my first recommendation is to refine your data structure because it is currently bloated with too many levels.
– mickmackusa
Nov 11 at 21:29
Your
sum_indirect()
is a custom function. I used a native recursive function.– mickmackusa
Nov 11 at 21:30
Your
sum_indirect()
is a custom function. I used a native recursive function.– mickmackusa
Nov 11 at 21:30
|
show 5 more comments
up vote
0
down vote
a simple way could be based on a couple for foreach one nested the other
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$myArray = json_decode($myJsonString, TRUE)
foreach($myArray['users'] as $value)
$result['sum_direct'] += $value['package']['direct'];
foreach($value['children']['package'] as $value2)
$result['sum_indirect'] += $value2['indirect'];
$jsonResult= json_encode($result);
@mickmackusa .. correct .. answer updated
– scaisEdge
Nov 10 at 7:48
@mickmackusa I updated with the attempts made at getting the summations.
– Bishoplee
Nov 10 at 21:24
add a comment |
up vote
0
down vote
a simple way could be based on a couple for foreach one nested the other
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$myArray = json_decode($myJsonString, TRUE)
foreach($myArray['users'] as $value)
$result['sum_direct'] += $value['package']['direct'];
foreach($value['children']['package'] as $value2)
$result['sum_indirect'] += $value2['indirect'];
$jsonResult= json_encode($result);
@mickmackusa .. correct .. answer updated
– scaisEdge
Nov 10 at 7:48
@mickmackusa I updated with the attempts made at getting the summations.
– Bishoplee
Nov 10 at 21:24
add a comment |
up vote
0
down vote
up vote
0
down vote
a simple way could be based on a couple for foreach one nested the other
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$myArray = json_decode($myJsonString, TRUE)
foreach($myArray['users'] as $value)
$result['sum_direct'] += $value['package']['direct'];
foreach($value['children']['package'] as $value2)
$result['sum_indirect'] += $value2['indirect'];
$jsonResult= json_encode($result);
a simple way could be based on a couple for foreach one nested the other
$result['sum_direct'] = 0;
$result['sum_indirect'] = 0;
$myArray = json_decode($myJsonString, TRUE)
foreach($myArray['users'] as $value)
$result['sum_direct'] += $value['package']['direct'];
foreach($value['children']['package'] as $value2)
$result['sum_indirect'] += $value2['indirect'];
$jsonResult= json_encode($result);
edited Nov 10 at 7:48
answered Nov 10 at 7:41
scaisEdge
89.9k94768
89.9k94768
@mickmackusa .. correct .. answer updated
– scaisEdge
Nov 10 at 7:48
@mickmackusa I updated with the attempts made at getting the summations.
– Bishoplee
Nov 10 at 21:24
add a comment |
@mickmackusa .. correct .. answer updated
– scaisEdge
Nov 10 at 7:48
@mickmackusa I updated with the attempts made at getting the summations.
– Bishoplee
Nov 10 at 21:24
@mickmackusa .. correct .. answer updated
– scaisEdge
Nov 10 at 7:48
@mickmackusa .. correct .. answer updated
– scaisEdge
Nov 10 at 7:48
@mickmackusa I updated with the attempts made at getting the summations.
– Bishoplee
Nov 10 at 21:24
@mickmackusa I updated with the attempts made at getting the summations.
– Bishoplee
Nov 10 at 21:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236788%2fhow-to-use-recursion-to-sum-specific-values-in-a-multidimensional-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
So what is the question? Read How to Ask and create a Minimal, Complete, and Verifiable example
– Alon Eitan
Nov 10 at 7:13
@Bishoplee When a question is Unclear or there is no evidence of effort, you will find that the question gets voted down. Please improve your question if you would like to prevent downvotes and run the chance of receiving upvotes.
– mickmackusa
Nov 10 at 22:35