After far too many hours troubleshooting what I expected to be a straight forward task, I believe I'm either missing an obvious flaw or have constructed an erroneous mental model of how it should work.
My actual data is much more complex, but I've simplified it down to this example.
Let's say I have an array $my_array of objects:
$object1 = new stdClass();
$object1->valueA = 'abc';
$object1->valueB = 'def';
$object1->valueC = '20160410';
$object2 = new stdClass();
$object2->valueA = '123';
$object2->valueB = '456';
$object2->valueC = '20160408';
$object3 = new stdClass();
$object3->valueA = 'foo';
$object3->valueB = 'bar';
$object3->valueC = '20160412';
$my_array = array(
'X' => $object1,
'Y' => $object2,
'Z' => $object3
);
I want to go through each object, use one of its values to calculate a new property, then assign the object to a new array, using the new calculate value to group them.
I did it this way:
$new_array= array();
foreach($my_array as $key=>$obj){
for($i=0;$i<=2;$i++){ //the real use case uses a slightly different loop, this is simpler/shorter for an example
$date = $obj->valueC + $i; //use valueC and the loop to calculate a new value
$obj->date = $date; //add my new value to the object
$new_array[$date][$key] = $obj; //construct new array of arrays of objects. bits on bits on bytes.
}
}
If I log the resulting array to Console, it looks like this:
[04-Apr-2016 22:09:28 UTC] Array
(
[20160410] => Array
(
[X] => stdClass Object
(
[valueA] => abc
[valueB] => def
[valueC] => 20160410
[date] => 20160412
)
[Y] => stdClass Object
(
[valueA] => 123
[valueB] => 456
[valueC] => 20160408
[date] => 20160410
)
)
[20160411] => Array
(
[X] => stdClass Object
(
[valueA] => abc
[valueB] => def
[valueC] => 20160410
[date] => 20160412
)
)
[20160412] => Array
(
[X] => stdClass Object
(
[valueA] => abc
[valueB] => def
[valueC] => 20160410
[date] => 20160412
)
[Z] => stdClass Object
(
[valueA] => foo
[valueB] => bar
[valueC] => 20160412
[date] => 20160414
)
)
[20160408] => Array
(
[Y] => stdClass Object
(
[valueA] => 123
[valueB] => 456
[valueC] => 20160408
[date] => 20160410
)
)
[20160409] => Array
(
[Y] => stdClass Object
(
[valueA] => 123
[valueB] => 456
[valueC] => 20160408
[date] => 20160410
)
)
[20160413] => Array
(
[Z] => stdClass Object
(
[valueA] => foo
[valueB] => bar
[valueC] => 20160412
[date] => 20160414
)
)
[20160414] => Array
(
[Z] => stdClass Object
(
[valueA] => foo
[valueB] => bar
[valueC] => 20160412
[date] => 20160414
)
)
)
Now, I would expect $new_array['20160410']['X']->date to be 20160410. After all, within 2 lines, I said, "use this value as the top-level array key and also as the value in the object". But no matter what I do, all instances of ['X'] will have the same date value. Same for ['Y'] and ['Z'].
My aim is to be able to store the newly calculated date value within the object while also using that value as a grouping key.