mohsin ali mohsin ali - 1 year ago 73
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


[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
for example
[anotherKey] => Another Value
string how do I do that?

Answer Source

You can try

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

    $anotherKey = "anotherKey";
    //To apply dynamic value 
    $result1[$key]->$anotherKey = "Another value";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download