I’m wondering how can I add attributes, that are not declared in a class, into an object.
Let me explain :
I have my order :
[0] => stdClass Object
(
[quantityBlack] => 3
[quantityBlue] => 1
[quantityGreen] => 0
[quantityOrange] => 0
[quantityPurple] => 0
[quantityRed] => 0
[dateOfOrder] => Fri, 06 Jul 12 22:21
[user_id] => 5
[comments] => Test
)
and I would like to replace the user_id by the attributes of the user,
so what I would like to do is :
foreach ($data['orders'] as $key => $order){
$data['orders']->$key???-> = $this->user_model->GetUsers(array('userId' => $order->user_id));
}
but I don’t know how to specifically target a single object (you see the key???) at the end, I would like to get the attributes of the related user to that order.
How can I do that ?
Thank You !
If I understand correctly your data structure you are seeking to do the following:
I’m assuming $data[‘orders’] is an array containing a bunch of stdClass objects like the one referenced in the question, hence why in this case you would use the $data[‘orders’][$key] syntax to access each array element first before accessing the properties of each object.
Note that both the data structure and your intent in the question are a bit unclear though, so if this doesn’t address your question you may want to give more context.