如何在数组中添加新项目
问题描述:
这是我在PHP中打印出的数组
This is a array that i printed out in PHP
Array ( [0] => Array ( [friend_id] => 1 [name] => parker[email] => parker@gmail.com)[1] => Array ( [friend_id] => 2 [name] => peter [email] => peter@hotmail.com) )
我能知道如何在php中使用循环在像这样的数组内添加新项/参数吗?
Can i know how to use loop in php to add a new item/param inside the array like this
Array ( [0] => Array ( [friend_id] => 1 [name] => parker[email] => parker@gmail.com [NEWOBJECT] => newvalue)[1] => Array ( [friend_id] => 2 [name] => peter [email] => peter@hotmail.com [NEWOBJECT] => newvalue) )
答
无需循环,您只需添加以下内容即可:
No need for loop you can just add by this:
<?
$arr = Array ( [0] => Array ( [friend_id] => 1 [name] => parker[email] => parker@gmail.com)[1] => Array ( [friend_id] => 2 [name] => peter [email] => peter@hotmail.com) );
$arr[0]['NEWOBJECT'] = 'blablabla';
$arr[1]['NEWOBJECT'] = 'blablabla';
?>
但是当您必须这样做两次以上时,这将有所帮助:
But when you have to do this more than 2 times of course this would help:
<?
$arr = Array ( [0] => Array ( [friend_id] => 1 [name] => parker[email] => parker@gmail.com)[1] => Array ( [friend_id] => 2 [name] => peter [email] => peter@hotmail.com) );
foreach($arr as $key => $value){
$arr[$key]['NEWOBJECT'] = 'blablabla';
}
?>