23

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?

themhz
  • 8,335
  • 21
  • 84
  • 109

2 Answers2

61

Simply set a new field:

$object->col4 = $value;

If you need dynamic field names:

$object->$fieldName = $value;
bwoebi
  • 23,637
  • 5
  • 58
  • 79
13

RE dynamic field names.

These should be defined as such.

$object->{$fieldName} = $value;
spoonerise
  • 131
  • 1
  • 2