目前,这是我的代码,但它不起作用:
//$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++;
}
我想转换这个输入数组($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"
}
]
进入这个(索引名称无关紧要,我把它放在那里是为了举例说明逻辑):
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"
}
]
基本上,我希望按年和月分组以获得余额。
ps:数据永远是有序的