0

我想在这里添加一些价值:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [cat_id] => 1
            [lot_id] => 1
            [brand] => Dell
            [model] => D630
            [unit_aed] => 800
            [sold] => 0
            ** i want to append value here like this
            [username] => name

        )

    [2] => stdClass Object
        (
            [id] => 4
            [cat_id] => 0
            [lot_id] => 0
            [brand] => dell
            [model] => e6400
            [unit_aed] => 0
            [sold] => 0
        )


)

这怎么可能?我正在尝试使用array_push($array, 'username').

它适用于简单数组,但不适用于stdClass对象数组。

4

3 回答 3

0
$array_name[0]->username = "name";
$array_name[0]->new_value = "whatever";
$array_name[0]->another_new_value = "whatever again";

ETC...

于 2012-10-04T10:38:10.460 回答
0

添加另一个对象键和值:

$array[0]->username = 'name';

或者您可以添加username到该对象中的每个值:

foreach($array as $key => $val){
    $array[$key]->username = 'user';
}
于 2012-10-04T10:38:37.633 回答
0
$myclass = new stdClass();
$myclass->id = 4;
$array[] = $myclass;
于 2012-10-04T10:44:23.537 回答