I would like to pre-define my object variables into an array. After that, I need to access one element at a time from that array and assign a value to it. Like so:
$obj = new StdClass();
$arr = ["$obj->item1"];
$arr[0] = "apple";
print_r($obj); // expecting to see $obj->item1 = apple
I know I'm doing something wrong above, but could not figure out what.
$arr[0], instead of assigning the property of $obj. What’s probably messing you up is that objects pass by reference; but arrays by value.$arr[0], then you might possibly be able to set$obj->item1somewhere else in the code, and then reference$arr[0]->item1.