php 我如何附加一个 stdClass 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16287111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do I append an stdClass Object
提问by themhz
I have an stdClass Object like generated by joomla like this
我有一个像这样由 joomla 生成的 stdClass 对象
$db->setQuery($sql);
$schoollist = $db->loadObjectList();
And the $schoollist variable contains the following stdClass Objects
并且 $schoollist 变量包含以下 stdClass 对象
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
我需要在查询后添加另一个“列”作为 [col4] => dsdads ,所以结果将是这样的
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?
我怎样才能做到这一点?
回答by bwoebi
Simply set a new field:
只需设置一个新字段:
$object->col4 = $value;
If you need dynamic field names:
如果您需要动态字段名称:
$object->$fieldName = $value;
回答by spoonerise
RE dynamic field names.
RE 动态字段名称。
These should be defined as such.
这些应该这样定义。
$object->{$fieldName} = $value;