我想给每个值,都添加一个元素c => 3,有没有系统函数啊
程序员文章站
2023-12-30 20:14:16
...
我想给每个值,都添加一个元素c => 3,不用foreach可以吗,有没有系统函数啊
$ary =
Array
(
[0] => Array
(
[a] => 1
[b] => 2
)
[1] => Array
(
[a] => 1
[b] => 2
)
...
)
目标:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
...
)
$ary =
Array
(
[0] => Array
(
[a] => 1
[b] => 2
)
[1] => Array
(
[a] => 1
[b] => 2
)
...
)
目标:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
...
)
回复讨论(解决方案)
先foreach大数组,然后foreach里面 array_push。
先foreach大数组,然后foreach里面 array_push。
不想用foreach啊。
我尝试用array_pad,但只会填充,不能增加。。。
$ary = array( array( 'a' => 1, 'b' => 2 ), array( 'a' => 1, 'b' => 2 ), array( 'a' => 1, 'b' => 2 ), );//方法一$res = array_map('array_merge', $ary, array_fill(0, count($ary), array('c' => 3)));print_r($res);//方法二function foo2(&$v) { $v['c'] = 3;}$res = $ary;array_walk($res, 'foo2');print_r($res);//方法三function foo3(&$v, $k, $param) { $v = array_merge($v, $param);}$res = $ary;array_walk($res, 'foo3', array('c' => 3));print_r($res);/*** php5.3以后还可以用闭包 **/