假设我正在循环这样的数组:
foreach($cursor as $obj) { ... }
,里面的值如下:
$obj['name'] => "foo"
$obj['surname'] => "test"
有没有办法直接从foreach中添加另一个键和值,而不使用'&'?像这样:
foreach($cursor as $obj) { $obj['age'] = 24; }
但不使用此:
foreach($cursor as &$obj) { $obj['age'] = 24; }
提前致谢。
答案 0 :(得分:6)
foreach($cursor as $k => $obj) {
$cursor[$k]['age'] = 24; //or whatever else you want to change it to
}