I want to add an element c & gt; 3 to each value. Is there any system function? I want to add an element c => 3 to each value. can I skip foreach, is there any system function?
$ Ary =
Array
(
[0] => Array
(
[A] => 1
[B] => 2
)
[1] => Array
(
[A] => 1
[B] => 2
)
...
)
Objectives:
Array
(
[0] => Array
(
[A] => 1
[B] => 2
[C] => 3
)
[1] => Array
(
[A] => 1
[B] => 2
[C] => 3
)
...
)
Reply to discussion (solution)
First, the foreach large array, and then the array_push in The foreach.
First, the foreach large array, and then the array_push in The foreach.
I don't want to use foreach.
I tried to use array_pad, but it only fills in and cannot be added...
$ Ary = array ('a' => 1, 'B' => 2), array ('a' => 1, 'B' => 2 ), array ('a' => 1, 'B' => 2),); // method 1 $ res = array_map ('Array _ merge', $ ary, array_fill (0, count ($ ary), array ('C' => 3); print_r ($ res); // method 2 function foo2 (& $ v) {$ v ['c'] = 3 ;}$ res = $ ary; array_walk ($ res, 'foo2'); print_r ($ res ); // method 3 function foo3 (& $ v, $ k, $ param) {$ v = array_merge ($ v, $ param);} $ res = $ ary; array_walk ($ res, 'foo3', array ('C' => 3); print_r ($ res);/*** you can use a closure after php5.3 **/