Currently, this is my code, but it doesn't work:
//$queryBalance is the array received from an sql select
foreach ($queryBalance as $index => $runrows){
$val = $runrows->balance;
$val = str_ireplace(".","",$val);
$val = str_ireplace(",",".",$val);
//echo($i.' - '.$runrows->id_month.' ; ');
//$valor = $runrows->id_month == $i ? $val : 'Null';
if($i > $monthFim){
$i = $monthInicio-1;
array_push($balanceArray, $balanceTeste);
echo('('.$i.' - '.$runrows->id_month.'); ');
//dd('('.$balanceArray.' - '.$balanceTeste.'); ');
}else{
array_push($balanceTeste, $valor);
}
$i++;
}
I want to transform this input array ($queryBalance):
array:6 [
0 => {
"balance": "-257,21"
"id_month": 1
"year": "2018"
}
1 => {
"balance": "-257,21"
"id_month": 2
"year": "2018"
}
2 => {
"balance": "0"
"id_month": 1
"year": "2018"
}
3 => {
"balance": "0"
"id_month": 2
"year": "2018"
}
4 => {
"balance": "-64609,14"
"id_month": 1
"year": "2018"
}
5 => {
"balance": "-64609,14"
"id_month": 2
"year": "2018"
}
]
into this (the indexes name is irrelevant, I put it there to exemplify the logic):
array:3 [
0 => {
"balance_month1_year2018": "-257,21"
"balance_month2_year2018": "-257,21"
}
1 => {
"balance_month1_year2018": "0"
"balance_month2_year2018": "0"
}
2 => {
"balance_month1_year2018": "-64609,14"
"balance_month2_year2018": "-64609,14"
}
]
Basically, I want group by year and month to get the balances.
ps: The data will always be ordered