Home  >  Article  >  Backend Development  >  我想给每个值,都添加一个元素c => 3,有没有系统函数啊

我想给每个值,都添加一个元素c => 3,有没有系统函数啊

WBOY
WBOYOriginal
2016-06-23 14:27:211084browse

我想给每个值,都添加一个元素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 
        )
    ...
)


回复讨论(解决方案)

先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以后还可以用闭包 **/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn