dqcqcqwq38860 2011-04-06 20:50
浏览 10
已采纳

更好的方法(可能使用array_map)

I have this arrays :

$a = array(
       'key1' => array ( 'k1'=>'value1', 'k2'=>'value2' , 'k3'='' ),
       'key2' => array ( 'k1'=>'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 ?

  • 写回答

1条回答 默认 最新

  • douwei1950 2011-04-06 21:09
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序