PHP 向每个二维数组追加元素

时间:2024-02-23 20:58:55

// 原数组

$data = [
    [
        \'first\'=>\'first_yuansu\',
        \'second\'=>\'qigemingzi\',  
    ],
    [
        \'third\'=>\'disange\',
        \'fourth\'=>\'last\',
    ]   
];

//追加数组

$addArr = [
    \'add\'=>\'test\'
];
array_walk($data, function (&$value, $key, $addArr) {
    $value = array_merge($value, $addArr);
}, $addArr);
print_r($data);

// 运行结果

Array
(
[0] => Array
    (
        [first] => first_yuansu
        [second] => qigemingzi
        [add] => test
    )
[1] => Array
    (
        [third] => disange
        [fourth] => last
        [add] => test
    )

)