将项添加到索引为0的php中的关联数组中

$result=mysql_query("SELECT * FROM users where id=1");
print_r($result);

Here is the result data from mysql query.

Array
(
    [0] => stdClass Object
        (
            [firstname] => "John"
            [middleinitial] => "A."
            [lastname] => "Doe"
        )
)

I want to add address: "USA" after the lastname that will result like this:

Array
(
    [0] => stdClass Object
        (
            [firstname] => "John"
            [middleinitial] => "A."
            [lastname] => "Doe"
            [address] => "USA"
        )
)

How can I append that to $result variable in php? Help would much appreciated. Tnx :)

This will work

  $result[0]->address = "USA";

This will work in one/more than one element case

$result = array_map(function ($v) {
    $v->address = "USA";
    return $v;
}, $result);

Give it a try. This should work.

You simply need to add a property to the object contained in the first index of your array,

$result[0]->address = 'USA';

Furthermore, if your array has multiple indices and you want to iterate through them all and add an address then you can do the following:

foreach ($result as &$row) {
    $row->address = 'USA';
}

where the & is to pass the $row variable into the loop by reference so that you can modify it.