将数组中的值转换为另一个多维数组
Transform values in an array to another multidimensional array
目前,这是我的代码,但它不起作用:
//$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: 数据总是有序的
生成一组唯一的年月组合。
(*声明为键以确保它们是唯一的)。
迭代$queryBalance
直到empty/exhausted.
(*如果你需要re-use原来的$queryBalance
复制一份.)
将$queryBalance
的第一个子数组指定为下一行的变量。
如果来自 $uniques
的预期日期对与 $first
日期对相同,则删除第一个子数组(通过 array_shift()
)并存储余额;否则存储 NULL
并且不修改 $queryBalance
.
$temp
是一个 "batching" 数组。当 foreach 循环完成时,批次已准备好转移到 $result
.
代码:(Demo)
$queryBalance=[
(object)["balance"=>"-257,21","id_month"=>12,"year"=>"2017"],
(object)["balance"=>"-257,21","id_month"=>1,"year"=>"2018"],
(object)["balance"=>"-257,21","id_month"=>2,"year"=>"2018"],
(object)["balance"=>"166,66","id_month"=>3,"year"=>"2018"],
(object)["balance"=>"55","id_month"=>12,"year"=>"2017"],
//(object)["balance"=>"0","id_month"=>1,"year"=>"2018"],
(object)["balance"=>"0","id_month"=>2,"year"=>"2018"],
(object)["balance"=>"55","id_month"=>3,"year"=>"2018"],
(object)["balance"=>"-22257,21","id_month"=>12,"year"=>"2017"],
(object)["balance"=>"-64609,14","id_month"=>1,"year"=>"2018"],
(object)["balance"=>"-64609,14","id_month"=>2,"year"=>"2018"],
(object)["balance"=>"-12234,89","id_month"=>3,"year"=>"2018"]
];
foreach($queryBalance as $row){
$uniques[$row->year.'-'.$row->id_month]=NULL; // determine all unique date groups
}
while($queryBalance){
$temp=[];
foreach($uniques as $date=>$null){
$first=current($queryBalance); // cache the element that is currently first in $queryBalance
$temp[]=($date==$first->year.'-'.$first->id_month) ? array_shift($queryBalance)->balance : NULL; // extract from array, or store NULL without modifying $queryBalance
}
$result[]=$temp;
}
var_export($result);
输出:
array (
0 =>
array (
0 => '-257,21',
1 => '-257,21',
2 => '-257,21',
3 => '166,66',
),
1 =>
array (
0 => '55',
1 => NULL,
2 => '0',
3 => '55',
),
2 =>
array (
0 => '-22257,21',
1 => '-64609,14',
2 => '-64609,14',
3 => '-12234,89',
),
)
目前,这是我的代码,但它不起作用:
//$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: 数据总是有序的
生成一组唯一的年月组合。
(*声明为键以确保它们是唯一的)。迭代
$queryBalance
直到empty/exhausted.
(*如果你需要re-use原来的$queryBalance
复制一份.)将
$queryBalance
的第一个子数组指定为下一行的变量。如果来自
$uniques
的预期日期对与$first
日期对相同,则删除第一个子数组(通过array_shift()
)并存储余额;否则存储NULL
并且不修改$queryBalance
.$temp
是一个 "batching" 数组。当 foreach 循环完成时,批次已准备好转移到$result
.
代码:(Demo)
$queryBalance=[
(object)["balance"=>"-257,21","id_month"=>12,"year"=>"2017"],
(object)["balance"=>"-257,21","id_month"=>1,"year"=>"2018"],
(object)["balance"=>"-257,21","id_month"=>2,"year"=>"2018"],
(object)["balance"=>"166,66","id_month"=>3,"year"=>"2018"],
(object)["balance"=>"55","id_month"=>12,"year"=>"2017"],
//(object)["balance"=>"0","id_month"=>1,"year"=>"2018"],
(object)["balance"=>"0","id_month"=>2,"year"=>"2018"],
(object)["balance"=>"55","id_month"=>3,"year"=>"2018"],
(object)["balance"=>"-22257,21","id_month"=>12,"year"=>"2017"],
(object)["balance"=>"-64609,14","id_month"=>1,"year"=>"2018"],
(object)["balance"=>"-64609,14","id_month"=>2,"year"=>"2018"],
(object)["balance"=>"-12234,89","id_month"=>3,"year"=>"2018"]
];
foreach($queryBalance as $row){
$uniques[$row->year.'-'.$row->id_month]=NULL; // determine all unique date groups
}
while($queryBalance){
$temp=[];
foreach($uniques as $date=>$null){
$first=current($queryBalance); // cache the element that is currently first in $queryBalance
$temp[]=($date==$first->year.'-'.$first->id_month) ? array_shift($queryBalance)->balance : NULL; // extract from array, or store NULL without modifying $queryBalance
}
$result[]=$temp;
}
var_export($result);
输出:
array (
0 =>
array (
0 => '-257,21',
1 => '-257,21',
2 => '-257,21',
3 => '166,66',
),
1 =>
array (
0 => '55',
1 => NULL,
2 => '0',
3 => '55',
),
2 =>
array (
0 => '-22257,21',
1 => '-64609,14',
2 => '-64609,14',
3 => '-12234,89',
),
)