I dont quite understand what is happening. copy the following code and run it you should see what I am seeing.
$stores = array(
(object)[
"store_id" => 1,
],
(object)[
"store_id" => 2,
],
(object)[
"store_id" => 3,
]
);
$currentYear = date('Y');
$monthes = array();
for($i = 1; $i <= 4; $i++){
$temp = new stdClass();
$temp->month = $i;
$temp->sales = 0;
array_push($monthes, $temp);
}
foreach($stores as $store){
$store->sales = array(
"currentYear" => (object)[
"year" => $currentYear,
"monthes" => $monthes,
],
);
}
foreach($stores as $store){
foreach($store->sales as $year){
foreach($year->monthes as $month){
$month->sales += 1;
}
}
}
print_r("<pre>");
print_r($stores);
print_r("</pre>");
the result it produces look like the following:
Array
(
[0] => stdClass Object
(
[store_id] => 1
[sales] => Array
(
[currentYear] => stdClass Object
(
[year] => 2018
[monthes] => Array
(
[0] => stdClass Object
(
[month] => 1
[sales] => 3
)
[1] => stdClass Object
(
[month] => 2
[sales] => 3
)
but I am expecting the sales to be 1. instead of 3. because it looks like it would visit each month just 1 time and sales' inital value is 0. so 0 += 1 should just be 1. Looks as if, it looped over itself 3 times.
I cant wrap my head around on what I did wrong here.