mohsin ali mohsin ali - 3 months ago 8
PHP Question

how to add another std object inside array in php

I am making an array of std objects in CodeIgniter

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


and I am getting the following result

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
)
}


Now I want to add one more key after
[sent_time]
for example
[anotherKey] => Another Value
string how do I do that?

Answer

You can try

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

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