function array_push_multi_assoc($array, $key, $key2, $value){
$array[$key][$key2] = $value;
return $array;
}
$myarray = array_push_multi_assoc($myarray, $key=0, 'subject', 'hello');
print_r($myarray);
Array
(
[0] => Array
(
[id] => 97
[email] => vikastyagismartbuzz8@gmail.com
[subject] => hello
)
[1] => Array
(
[id] => 93
[email] => vikastyagi87@gmail.com
)
[2] => Array
(
[id] => 94
[email] => vikastyagismartbuzz9@gmail.com
)
)
我希望有类似的东西:
Array
(
[0] => Array
(
[id] => 97
[email] => vikastyagismartbuzz8@gmail.com
[subject] => hello
)
[1] => Array
(
[id] => 93
[email] => vikastyagi87@gmail.com
[subject] => hello
)
[2] => Array
(
[id] => 94
[email] => vikastyagismartbuzz9@gmail.com
[subject] => hello
)
)
答案 0 :(得分:1)
array_map可用于构建新数组:
$newArray = array_map(function($row) {
$row['subject'] = 'hello';
return $row;
}, $oldArray);
或使用array_walk来操纵现有的:
array_walk($oldArray, function(&$row) {
$row['subject'] = 'hello';
});
注意,你可以改变这些函数中的任何一个,这意味着它们的角色是相反的(例如用array_walk构建一个新数组),但上面的例子是每个函数应该如何使用。
答案 1 :(得分:0)
虽然它可能是通过循环元素实现的,但更适用和更强大的方法是使用array_map
:
array_map(function($item) { $item['subject'] = 'hello'; }, $array)
希望它有所帮助。