基本上我有这个数组$sales
:
Array
(
[0] => Array
(
[orderCount] => 3
[products] => 4
[year] => 2014
[month] => 11
)
[1] => Array
(
[orderCount] => 1
[products] => 2
[year] => 2014
[month] => 12
)
)
我想用这个数组$months
来填充未完成的月份:
Array
(
[1] => Janv
[2] => Fév
[3] => Mars
[4] => Avr
[5] => Mai
[6] => Juin
[7] => Juil
[8] => Aout
[9] => Sept
[10] => Oct
[11] => Nov
[12] => Déc
)
以某种方式生成这样的数组:
Array
(
[0] => Array
(
[orderCount] => 0
[products] => 0
[year] => 2014
[month] => Janv
)
.
.
[10] => Array
(
[orderCount] => 3
[products] => 4
[year] => 2014
[month] => Nov
)
[11] => Array
(
[orderCount] => 1
[products] => 2
[year] => 2014
[month] => Déc
)
)
到目前为止,我们尝试了什么并且没有工作:
$result = array();
foreach ($month as $key => $value) {
foreach ($sales as $k => $v) {
if( $v['month']==$key){
$result[] = array(
"orderCount" => $v['orderCount'],
"products" => $v['products'],
"month" => $value,
"year" => $v['year']
);
}else{
$result[] = array(
"orderCount" => "0",
"products" => "0",
"month" => $value,
"year" => $v['year']
);
}
}
}
答案 0 :(得分:0)
在这个Demo上扔了几件事。
本质上基本上是这样的:
$new = array_map(function($row) use($month) {
$row['month'] = $month[$row['month']];
return $row;
}, $sales);
它迭代$ sales数组和"修复"每月通过在$ month中查找其int值作为索引并返回修改后的数组。
答案 1 :(得分:0)
试试这个..
$aa = array(
array("orderCount"=>3,"products"=>4,"year"=>2014,"month"=>0),
array("orderCount"=>3,"products"=>4,"year"=>2014,"month"=>1),
);
$bb=array("jan","Fév");
foreach($aa as $key=>$val)
{
$month= $val['month'];
$aa[$key]['month']=$bb[$month];
}
print_r($aa);
<强>结果:强>
Array ( [0] => Array ( [orderCount] => 3 [products] => 4 [year] => 2014 [month] => jan ) [1] => Array ( [orderCount] => 3 [products] => 4 [year] => 2014 [month] => Fév ) )