21

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
  • 7,841
  • 19
  • 76
  • 106

2 Answers2

55

Simply set a new field:

$object->col4 = $value;

If you need dynamic field names:

$object->$fieldName = $value;
bwoebi
  • 22,955
  • 4
  • 54
  • 75
13

RE dynamic field names.

These should be defined as such.

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