我有一个二维数组:
$test = array(
"foo" => array(
'a' => 1,
'b' => 2,
'c' => 3
),
"bar" => array(
'a' => 1,
'b' => 2,
'c' => 3
),
"baz" => array(
'a' => 1,
'b' => 2,
'c' => 3
)
);
我想在外部数组的每个元素中添加一个以'd'
值命名的字段4
,以便生成的数组变为:
array(
"foo" => array(
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4
),
"bar" => array(
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4
),
"baz" => array(
'a' => 1,
'b' => 2,
'c' => 3,
'd' => 4
)
)
我试过这个:
foreach ( $test as $elem )
{
$elem['d'] = 4;
}
这是行不通的。我做错了什么,我怎样才能做到这一点?