Well basically I have this array $sales
:
Array
(
[0] => Array
(
[orderCount] => 3
[products] => 4
[year] => 2014
[month] => 11
)
[1] => Array
(
[orderCount] => 1
[products] => 2
[year] => 2014
[month] => 12
)
)
I would like to fill to complete unexisted months with this array $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
)
In a way to generate an array like this :
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
)
)
And here's what've tried so far and didn't work :
$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']
);
}
}
}
T threw a few things together at this Demo.
The essence is basically this:
$new = array_map(function($row) use($month) {
$row['month'] = $month[$row['month']];
return $row;
}, $sales);
it iterates the $sales array and "fixes" each month by looking up its int value as index in $months and returning a modified array.
Try this..
$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);
result:
Array ( [0] => Array ( [orderCount] => 3 [products] => 4 [year] => 2014 [month] => jan ) [1] => Array ( [orderCount] => 3 [products] => 4 [year] => 2014 [month] => Fév ) )