简体   繁体   English

如何添加到stdObject数组?

[英]How to add to stdObject array?

Here is the result of print_r($object): 这是print_r($ object)的结果:

Array
(
    [0] => stdClass Object
      (
        [title] => Test procedures in watermelons
        [nid] => 494
        [type] => chsmanuscript
      )

    [1] => stdClass Object
      (
        [title] => How to eat cookies
        [nid] => 520
        [type] => chsmanuscript
      )
)

How can I manually add a 3rd object item in this object array? 如何在此对象数组中手动​​添加第三个对象项? I have tried the below but it does not work: 我已经尝试了以下方法,但是它不起作用:

$object[2]->title = 'test';
$object[2]->nid   = '999';
$object[2]->type  = 'chsmanuscript'

The error is: Fatal error: Cannot use object of type DatabaseStatementBase as array 错误是:致命错误:不能将DatabaseStatementBase类型的对象用作数组

You must first create a new stdClass object in the specific array element before you try to access it's members: 您必须先在特定的数组元素中创建一个新的stdClass对象,然后才能尝试访问其成员:

$object[2] = new stdClass();
$object[2]->title = 'test';
$object[2]->nid   = '999';
$object[2]->type  = 'chsmanuscript'
 $object[] = (object)array('title'=>'test', 
                   'ndid'=>'999',
                   'type'=> 'chsmanuscript');

This pushes to the array , if you need it to push at the third position, specify that in in $object[#] 这将推送到数组,如果需要将其推送到第三个位置,请在$ object [#]中指定

您也可以这样做

$object[2] = (object)array('title'=>'test', 'nid'=>'999', 'type'=>'chsmanuscript');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM