如何在php中的数组中添加另一个std对象

时间:2016-09-05 10:48:26

标签: php arrays codeigniter object

我在CodeIgniter中创建一个std对象数组

$result1 = $this->db->query($query1)->result();

我得到以下结果

结果

Array
   (
    [0] => stdClass Object
      (
        [message_id] => 10
        [sender] => 22
        [receiver] => 24
        [message] => hello atif
        [sent_date] => 02-09-2016
        [sent_time] => 10:12:15am
    )  
 }

现在我想在[sent_time]之后再添加一个密钥,例如[anotherKey] => Another Value字符串,我该怎么做?

2 个答案:

答案 0 :(得分:1)

你可以尝试

foreach($result1 as $key=>$value)
{
    $result1[$key]->anotherKey = "Another value";

    $anotherKey = "anotherKey";
    //To apply dynamic value 
    $result1[$key]->$anotherKey = "Another value";
}

答案 1 :(得分:0)

您还可以像这样扩展对象:

$obj = new stdClass;
$obj->anotherKey = "Another Value";

或循环:

foreach ($stdClass as $key => $value) {
    $stdClass[$key]->anotherKey = "Another Value";
}