Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中如何在多维数组中追加值_Php_Arrays_Multidimensional Array - Fatal编程技术网

在php中如何在多维数组中追加值

在php中如何在多维数组中追加值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想在这里增加一些价值: Array ( [0] => stdClass Object ( [id] => 1 [cat_id] => 1 [lot_id] => 1 [brand] => Dell [model] => D630 [unit_aed] => 800

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

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\u push($array,'username')

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

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

等等。

添加另一个对象键和值:

$array[0]->username = 'name';
或者您可以向该对象中的每个值添加
username

foreach($array as $key => $val){
    $array[$key]->username = 'user';
}
$myclass = new stdClass();
$myclass->id = 4;
$array[] = $myclass;