duanche4578 2015-08-31 14:31
浏览 53

在php中合并多个数组

Here is a sample array values that is returning:

Here is the first array:

Array 1:

     0 => 
      name => Test name value
      desrciption => Test description value
      category => Test category value
      code_1 => IGEF001
      code_2 => IGGF001

     1 => 
      name => Test name value
      desrciption => Test description value
      category => Test category value
      code_1 => IGEF003
      code_2 => IGGF003

     2 => 
       name => Test name value
       desrciption => Test description value
       category => Test category value
       code_1 => IGEF004
       code_2 => IGGF004

Here is the second array:

Array 2:

 0 => 
    return_code => IGEF003

 1 => 
   return_code => IGGF003

 2 => 
   return_code => IGGF004

 3 => 
   return_code => IGEF004

 4 => 
   return_code => IGGF001

 5 => 
   return_code => IGEF001

Here is what I'm trying to accomplish:

 0 => 
   name => Test name value
   desrciption => Test description value
   category => Test category value
   code_1 => IGEF001
   code_2 => IGGF001
   select_code_1 => IGEF001 <-- Value coming from the second array
   select_code_2 => IGGF001 <-- Value coming from the second array


 1 => 
  name => Test name value
  desrciption => Test description value
  category => Test category value
  code_1 => IGEF003
  code_2 => IGGF003
  select_code_1 => IGEF003 <-- Value coming from the second array
  select_code_2 => IGGF003 <-- Value coming from the second array

 2 => 
  name => Test name value
  desrciption => Test description value
  category => Test category value
  code_1 => IGEF004
  code_2 => IGGF004
  select_code_1 => IGEF004 <-- Value coming from the second array
  select_code_2 => IGGF004 <-- Value coming from the second array

I hope this is enough information, let me know if you need any more.

  • 写回答

2条回答 默认 最新

  • dongming5444 2015-08-31 14:39
    关注

    Well, if I understood your question correctly, you want to find matching entries in the second array and add them to the first array. For the purpose of keeping this example simple, I assume that matching entries always exist. If that's not the case, you need to add an if with array_key_exists() or something.

    $result = array();
    foreach ($array1 as $key => $value) {
       $result = $value;
       $result['select_code'] = $array2[$key]['return_code'];
    }
    

    What I could not figure out is where the second "select_code" entry in the result should be found. The only way to map entries in the first with entries in the second array is the key, as far as I can tell. If you provide more insight on the nature of your data, I will edit my answer.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c