开发者

a better way to do this (probably using array_map)

开发者 https://www.devze.com 2023-02-22 08:36 出处:网络
I have this arrays : $a = array( \'key1\' => array ( \'k1\'=>\'value1\', \'k2\'=>\'value2\' , \'k3\'=\'\' ),

I have this arrays :

$a = array(
       'key1' => array ( 'k1'=>'value1', 'k2'=>'value2' , 'k3'='' ),
       'key2' => array ( 'k1'=开发者_Go百科>'value1', 'k2'=>'value2' , 'k3'='' ),
       ...
       );

and I have another array:

$b = array('key1'=>'value array b key 1'),
           'key2'=>'value array b key 2'),
           ...
          );

I need to get the values from array $b and put in array $a in key 'k3', I am using this function to do this:

foreach($a as $key => $item) {
    $a[$key]['key3'] = $b[$key];
}

Its works for me, but I d like to know if there is a better and simplier way to do that, something using array_map....

Anyone has some idea ?


Your code is way easier to understand. The array_walk(not map, since the return value does not matter) equivalent would be

array_walk($b, function($el, $k, $a) {$a[$k]['key3'] = $el;}, &$a);
0

精彩评论

暂无评论...
验证码 换一张
取 消