代码如下图:这样子在foreach中是无法修改class_history的。正确的应该是去掉??[]判断。
public function actionY()
{
$array = [
'name'=>'aaa',
'class_history'=>[
[
'class_name'=>'一班',
'class_num'=>1
],
[
'class_name'=>'二班',
'class_num'=>2
]
]
];
foreach ($array['class_history']??[] as &$item) {
$item['class_name'] = '三班';
$item['class_num'] = 3;
}
var_dump($array);
}