duanrong3308 2014-07-03 14:48
浏览 43
已采纳

如何将两个数组的第二级数值与不同数量的键合并?

I have following arrays:

$array1 = "
    Array (
        [0] => Array (
                   [0] => Value1
                   [1] => Value2
               )

        [1] => Array (
                   [0] => Value3
                   [1] => Value4
               )

        [2] => Array (
                   [0] => Value5
                   [1] => Value6
               )
        ...
        [999] => Array (
                   [0] => Value7
                   [1] => Value8
               )
    )

and

$array2 = "
    Array (
        [0] => Array (
                   [0] => ValA
                   [1] => ValB
                   [2] => ValC
                   [3] => ValD
               )

        [1] => Array (
                   [0] => ValE
                   [1] => ValF
                   [2] => ValG
                   [3] => ValH
               )

        [2] => Array (
                   [0] => ValI
                   [1] => ValJ
                   [2] => ValK
                   [3] => ValL
               )
        ...
        [999] => Array (
                   [0] => ValM
                   [1] => ValN
                   [2] => ValO
                   [3] => ValP
               )
    )

I would like to merge both arrays so that in the end I have the same structure, number of keys in the first level, but the values would be "joined" in the second level just like we can see in the following result array:

$array3 = "
    Array (
        [0] => Array (
                   [0] => Value1
                   [1] => Value2
                   [2] => ValA
                   [3] => ValB
                   [4] => ValC
                   [5] => ValD
               )

        [1] => Array (
                   [0] => Value3
                   [1] => Value4
                   [2] => ValE
                   [3] => ValF
                   [4] => ValG
                   [5] => ValH
               )

        [2] => Array (
                   [0] => Value5
                   [1] => Value6
                   [2] => ValI
                   [3] => ValJ
                   [4] => ValK
                   [5] => ValL
               )
        ...
        [999] => Array (
                   [0] => Value7
                   [1] => Value8
                   [2] => ValM
                   [3] => ValN
                   [4] => ValO
                   [5] => ValP
               )
    )

I tried with array_merge and array_merge_recursive as showed in this solution: https://stackoverflow.com/a/16541831/3499881, but it didn't work.

  • Do I really have to convert all numeric keys to string or is it somehow also possible with that keys? If this is the only way, could you please show me how I can convert it?

  • Or is the problem caused because in the first array there are 2 keys and in the second 4?

I would appreciate any clue. Many thanks in advance.

  • 写回答

2条回答 默认 最新

  • dspx15491 2014-07-03 15:00
    关注

    There is an "elegant" way to resolve your issue (it will require, however, that both of your arrays must have same count of items):

    $result = array_map('array_merge', $array1, $array2);
    

    Check the fiddle.

    Answering to your question: array_merge_recursive() will fail because it appends numeric keys, not merging them:

    If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?