-3

假设我正在循环一个这样的数组:

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; }

提前致谢。

4

1 回答 1

7
foreach($cursor as $k => $obj) { 
    $cursor[$k]['age'] = 24; //or whatever else you want to change it to
}
于 2012-11-23T15:55:17.007 回答