I have the following Array
multidimensional:
[
[
{
"l_grupo": "F003"
},
{
"d_anul": "0", "c_comp": "01", "n_seri": "F003", "n_comp": "45", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "375.0000000"
},
{
"d_anul": "0", "c_comp": "01", "n_seri": "F003", "n_comp": "46", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "52.0000000"
},
{
"d_anul": "0", "c_comp": "01", "n_seri": "F003", "n_comp": "47", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "116.0000000"
}
],
[
{
"l_grupo": "B003"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "21", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "35.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "18", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "88.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "15", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "93.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "16", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "112.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "17", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "78.0000000"
}
]
]
And I want to get the sum of the field s_tota
for each group and add it to the index 0
where the field is l_grupo
and get the following result:
[
[
{
"l_grupo": "F003", "total": "543.00"
},
{
"d_anul": "0", "c_comp": "01", "n_seri": "F003", "n_comp": "45", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "375.0000000"
},
{
"d_anul": "0", "c_comp": "01", "n_seri": "F003", "n_comp": "46", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "52.0000000"
},
{
"d_anul": "0", "c_comp": "01", "n_seri": "F003", "n_comp": "47", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "116.0000000"
}
],
[
{
"l_grupo": "B003" , "total": "406"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "21", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "35.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "18", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "88.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "15", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "93.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "16", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "112.0000000"
},
{
"d_anul": "0", "c_comp": "03", "n_seri": "B003", "n_comp": "17", "f_comp": "2018-08-04 00:00:00.000", "k_mone": "0", "s_tipc": "1.0000000", "s_tota": "78.0000000"
}
]
]
I have tried in many ways but I have not been able to achieve it.
First of all, you must go through the array to process the sum and add the total property to each element, it would be as follows:
We help each other in each return of the first
form
of an accumulator, for later, inside and nested in the firstfor
, we go from position number 1 (not from zero because here there is nothing to get and because there is never a way to add as_tota
) to add each property it finds withs_tota
. At the end, to the same array, we add the propertytotal
as I explained to you from the beginning.I hope it helps you.
you can do it like this:
You could use foreach's would be more useful: