how do I append an stdClass Object
Asked Answered
T

2

24

I have an stdClass Object like generated by joomla like this

$db->setQuery($sql);
$schoollist = $db->loadObjectList(); 

And the $schoollist variable contains the following stdClass Objects

stdClass Object ( [id] => 1 [col1] => blabla [col2] => 5 [col3] => 208 ) 
stdClass Object ( [id] => 2 [col1] => test1 [col2] => 1 [col3] => 52 ) 

and I need to add another "column" after the query as [col4] => dsdads , so the result will be like this

stdClass Object ( [id] => 1 [col1] => blabla [col2] => 5 [col3] => 208 [col4] => 208) 
stdClass Object ( [id] => 2 [col1] => test1 [col2] => 1 [col3] => 52 [col4] => 208) 

how can I do this?

Thousandth answered 29/4, 2013 at 19:55 Comment(0)
B
62

Simply set a new field:

$object->col4 = $value;

If you need dynamic field names:

$object->$fieldName = $value;
Bacterium answered 29/4, 2013 at 19:57 Comment(0)
T
13

RE dynamic field names.

These should be defined as such.

$object->{$fieldName} = $value;
Twoup answered 16/1, 2016 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.