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
)
)
I want to have something like that:
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
)
)
array_map can be used to build a new array, after applying a function to an existing one:
$newArray = array_map(function($row) {
$row['subject'] = 'hello';
return $row;
}, $oldArray);
Or use array_walk to manipulate the existing one:
array_walk($oldArray, function(&$row) {
$row['subject'] = 'hello';
});
Note, you can rewrite either of these functions in a way that will mean their roles are reversed (e.g. building a new array with array_walk) but the examples above are how each should be used.
Though it might have been achieved with loop through elements, the more applicable and powerful way is to use array_map
:
array_map(function($item) { $item['subject'] = 'hello'; }, $array)
Hope it helps.