adding end values in a multidimensional array
up vote
-2
down vote
favorite
I have an array with the following structure.
Is there a function which can combine elements together, summing the totals?
Thanks for any pointers on how to achieve this. I've included a section of the array here, and then lower down the hopeful result.
I can achieve this via creating a second array as I reinterate over a php mysql query, but this doesn't feel like a very efficient way of doing things.
Array
(
[1] => Array
(
[Y01MA] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 4
[2] => 6
[3] => 1
)
[M] => Array
(
[1] => 5
[2] => 5
)
)
[P] => Array
(
[F] => Array
(
[2] => 4
[3] => 6
[4] => 1
)
[M] => Array
(
[1] => 1
[2] => 4
[3] => 5
)
)
)
[Y01MB] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 7
[2] => 2
)
[M] => Array
(
[1] => 11
[2] => 1
[3] => 1
)
)
[P] => Array
(
[F] => Array
(
[2] => 3
[3] => 6
)
[M] => Array
(
[2] => 3
[3] => 9
[4] => 1
)
)
)
)
Ideally what I would then end up with is something like this
Array
(
[1] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 18
[2] => 2
[3] => 0
[4] => 0
)
[M] => Array
(
[1] => 23
[2] => 0
[3] => 0
[4] => 0
)
)
[P] => Array
(
[F] => Array
(
[1] => 1
[2] => 11
[3] => 7
[4] => 1
)
[M] => Array
(
[1] => 4
[2] => 11
[3] => 8
[4] => 0
)
)
)
php arrays
add a comment |
up vote
-2
down vote
favorite
I have an array with the following structure.
Is there a function which can combine elements together, summing the totals?
Thanks for any pointers on how to achieve this. I've included a section of the array here, and then lower down the hopeful result.
I can achieve this via creating a second array as I reinterate over a php mysql query, but this doesn't feel like a very efficient way of doing things.
Array
(
[1] => Array
(
[Y01MA] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 4
[2] => 6
[3] => 1
)
[M] => Array
(
[1] => 5
[2] => 5
)
)
[P] => Array
(
[F] => Array
(
[2] => 4
[3] => 6
[4] => 1
)
[M] => Array
(
[1] => 1
[2] => 4
[3] => 5
)
)
)
[Y01MB] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 7
[2] => 2
)
[M] => Array
(
[1] => 11
[2] => 1
[3] => 1
)
)
[P] => Array
(
[F] => Array
(
[2] => 3
[3] => 6
)
[M] => Array
(
[2] => 3
[3] => 9
[4] => 1
)
)
)
)
Ideally what I would then end up with is something like this
Array
(
[1] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 18
[2] => 2
[3] => 0
[4] => 0
)
[M] => Array
(
[1] => 23
[2] => 0
[3] => 0
[4] => 0
)
)
[P] => Array
(
[F] => Array
(
[1] => 1
[2] => 11
[3] => 7
[4] => 1
)
[M] => Array
(
[1] => 4
[2] => 11
[3] => 8
[4] => 0
)
)
)
php arrays
Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12
ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15
Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have an array with the following structure.
Is there a function which can combine elements together, summing the totals?
Thanks for any pointers on how to achieve this. I've included a section of the array here, and then lower down the hopeful result.
I can achieve this via creating a second array as I reinterate over a php mysql query, but this doesn't feel like a very efficient way of doing things.
Array
(
[1] => Array
(
[Y01MA] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 4
[2] => 6
[3] => 1
)
[M] => Array
(
[1] => 5
[2] => 5
)
)
[P] => Array
(
[F] => Array
(
[2] => 4
[3] => 6
[4] => 1
)
[M] => Array
(
[1] => 1
[2] => 4
[3] => 5
)
)
)
[Y01MB] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 7
[2] => 2
)
[M] => Array
(
[1] => 11
[2] => 1
[3] => 1
)
)
[P] => Array
(
[F] => Array
(
[2] => 3
[3] => 6
)
[M] => Array
(
[2] => 3
[3] => 9
[4] => 1
)
)
)
)
Ideally what I would then end up with is something like this
Array
(
[1] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 18
[2] => 2
[3] => 0
[4] => 0
)
[M] => Array
(
[1] => 23
[2] => 0
[3] => 0
[4] => 0
)
)
[P] => Array
(
[F] => Array
(
[1] => 1
[2] => 11
[3] => 7
[4] => 1
)
[M] => Array
(
[1] => 4
[2] => 11
[3] => 8
[4] => 0
)
)
)
php arrays
I have an array with the following structure.
Is there a function which can combine elements together, summing the totals?
Thanks for any pointers on how to achieve this. I've included a section of the array here, and then lower down the hopeful result.
I can achieve this via creating a second array as I reinterate over a php mysql query, but this doesn't feel like a very efficient way of doing things.
Array
(
[1] => Array
(
[Y01MA] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 4
[2] => 6
[3] => 1
)
[M] => Array
(
[1] => 5
[2] => 5
)
)
[P] => Array
(
[F] => Array
(
[2] => 4
[3] => 6
[4] => 1
)
[M] => Array
(
[1] => 1
[2] => 4
[3] => 5
)
)
)
[Y01MB] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 7
[2] => 2
)
[M] => Array
(
[1] => 11
[2] => 1
[3] => 1
)
)
[P] => Array
(
[F] => Array
(
[2] => 3
[3] => 6
)
[M] => Array
(
[2] => 3
[3] => 9
[4] => 1
)
)
)
)
Ideally what I would then end up with is something like this
Array
(
[1] => Array
(
[O] => Array
(
[F] => Array
(
[1] => 18
[2] => 2
[3] => 0
[4] => 0
)
[M] => Array
(
[1] => 23
[2] => 0
[3] => 0
[4] => 0
)
)
[P] => Array
(
[F] => Array
(
[1] => 1
[2] => 11
[3] => 7
[4] => 1
)
[M] => Array
(
[1] => 4
[2] => 11
[3] => 8
[4] => 0
)
)
)
php arrays
php arrays
edited Nov 8 at 12:41
asked Nov 8 at 11:09
jack
104
104
Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12
ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15
Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18
add a comment |
Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12
ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15
Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18
Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12
Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12
ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15
ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15
Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18
Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need a recursive sum of array.
You can achieve this by a recursively reducing array using array_reduce function.
function sumRecursive($carry, $item) {
if(is_array($item)){
return array_reduce($item, 'sumRecursive', $carry);
}
return $carry+$item ;
}
$sum = sumRecursive(0, $arr);
Here's example working code: https://3v4l.org/FuGtf
Other way would to be utilize RecursiveArrayIterator and RecursiveIteratorIterator to iterate over the first one. ;-)
$sum = array_sum(
iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveArrayIterator($arr)
),
false
)
);
Working code: https://3v4l.org/gEB6q
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need a recursive sum of array.
You can achieve this by a recursively reducing array using array_reduce function.
function sumRecursive($carry, $item) {
if(is_array($item)){
return array_reduce($item, 'sumRecursive', $carry);
}
return $carry+$item ;
}
$sum = sumRecursive(0, $arr);
Here's example working code: https://3v4l.org/FuGtf
Other way would to be utilize RecursiveArrayIterator and RecursiveIteratorIterator to iterate over the first one. ;-)
$sum = array_sum(
iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveArrayIterator($arr)
),
false
)
);
Working code: https://3v4l.org/gEB6q
add a comment |
up vote
1
down vote
accepted
You need a recursive sum of array.
You can achieve this by a recursively reducing array using array_reduce function.
function sumRecursive($carry, $item) {
if(is_array($item)){
return array_reduce($item, 'sumRecursive', $carry);
}
return $carry+$item ;
}
$sum = sumRecursive(0, $arr);
Here's example working code: https://3v4l.org/FuGtf
Other way would to be utilize RecursiveArrayIterator and RecursiveIteratorIterator to iterate over the first one. ;-)
$sum = array_sum(
iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveArrayIterator($arr)
),
false
)
);
Working code: https://3v4l.org/gEB6q
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need a recursive sum of array.
You can achieve this by a recursively reducing array using array_reduce function.
function sumRecursive($carry, $item) {
if(is_array($item)){
return array_reduce($item, 'sumRecursive', $carry);
}
return $carry+$item ;
}
$sum = sumRecursive(0, $arr);
Here's example working code: https://3v4l.org/FuGtf
Other way would to be utilize RecursiveArrayIterator and RecursiveIteratorIterator to iterate over the first one. ;-)
$sum = array_sum(
iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveArrayIterator($arr)
),
false
)
);
Working code: https://3v4l.org/gEB6q
You need a recursive sum of array.
You can achieve this by a recursively reducing array using array_reduce function.
function sumRecursive($carry, $item) {
if(is_array($item)){
return array_reduce($item, 'sumRecursive', $carry);
}
return $carry+$item ;
}
$sum = sumRecursive(0, $arr);
Here's example working code: https://3v4l.org/FuGtf
Other way would to be utilize RecursiveArrayIterator and RecursiveIteratorIterator to iterate over the first one. ;-)
$sum = array_sum(
iterator_to_array(
new RecursiveIteratorIterator(
new RecursiveArrayIterator($arr)
),
false
)
);
Working code: https://3v4l.org/gEB6q
answered Nov 8 at 11:31
Jakub Matczak
11.7k43448
11.7k43448
add a comment |
add a comment |
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%2f53206543%2fadding-end-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
Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12
ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15
Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18