I've inherited a codebase where I found something a refined and "boiled" version of which is below
$json = '[ {"id": 1, "val":"apple"}, {"id": 2, "val":"mango"} ]';
$data = json_decode($json);
$arr = [];
foreach($data as $d){
$m->id = $d->id;
$m->val = $d->val;
$arr[] = $m;
}
print_r($arr);
Result
Array
(
[0] => stdClass Object
(
[id] => 2
[val] => mango
)
[1] => stdClass Object
(
[id] => 2
[val] => mango
)
)
My question is why would the current iteration have any effect on its predecessor?
PHP version : 7.2
EDIT : Apologies if the problem looks related to json but it isn't, instead in my codebase I have these objects coming from another called function. I have used json_decode merely to illustrate the problem