0

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.

1 Answer 1

2

You're storing the same $monthes array into each of the currentYear objects. While the array is copied when you assign it, the objects that it contains are not; all these arrays contain references to the same four objects. So when you increment the sales in store 1 month 1, it also increments store 2 month 1, store 3 month 1, and store 4 month 1.

You need to put the loop that creates the $monthes array inside the loop that fills in each of the stores.

<?php
$stores = array(
    (object)[
        "store_id" => 1,
        ],
    (object)[
        "store_id" => 2,
        ],
    (object)[
        "store_id" => 3,
        ]
    );

$currentYear = date('Y');
foreach($stores as $store){
    $monthes = array();
    for($i = 1; $i <= 4; $i++){
        $temp = new stdClass();
        $temp->month = $i;
        $temp->sales = 0;
        array_push($monthes, $temp);
    }
    $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;
        }
    }
}

echo "<pre>";
print_r($stores);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.