For object created from array foreach cycle go through first item twice
$list = (object)['a' => 1, 'b' => 2];
echo json_encode($list);
$pointers = [];
foreach($list as $n => $v)
$pointers[] = &$list->$n;
var_dump($pointers);
json returns 2 items, pointers at end returns 3 items. What can be wrong?
But if I create object as stdClass, it works as expected.
$list = new stdClass();
$list->a = 1;
$list->b = 2;
echo json_encode($list);
$pointers = [];
foreach($list as $n => $v)
$pointers[] = &$list->$n;
var_dump($pointers);
json returns 2 items, pointers at end returns 2 items
$list->$nin your first example you correctly have 2 items. Maybe there is some additional pointer defined when casting to (object). Need to check that out.{"a":1,"b":2}array(3) { [0]=> &int(1) [1]=> &int(1) [2]=> &int(2) }but it works fine in php 5.6