douya1855 2013-07-19 03:06
浏览 34
已采纳

php - 如何合并每个数组具有不同元素的2d数组

I have 2 set of 2d array and i want merge into 1 2d array. but the number of element in each array its not same and for the first 2 element is same and i don't want to duplicate it. here its is.

First 2d array:

Array(   
       [0] => Array
           (
              [0] => 25/2/2013
              [1] => 8.45 a.m
              [2] => 9.98
           )

       [1] => Array
           (
              [0] => 25/2/2013
              [1] => 8.46 a.m
              [2] => 9.02
           )
     )

second 2d array:

 Array(   
        [0] => Array
            (
                [0] => 25/2/2013
                [1] => 8.45 a.m
                [2] => 1.23
                [3] => 6.1
            )

        [1] => Array
            (
                [0] => 25/2/2013
                [1] => 8.46 a.m
                [2] => 1.75
                [3] => 1.75
            )
      )

How do i get result as this:

Array(   
        [0] => Array
            (
                [0] => 25/2/2013
                [1] => 8.45 a.m
                [2] => 9.98
                [3] => 1.23
                [4] => 6.1
            )

        [1] => Array
            (
                [0] => 25/2/2013
                [1] => 8.46 a.m
                [2] => 9.02
                [3] => 1.75
                [4] => 1.75
            )
     )

here is var export for first array:

( 0 => array ( 0 => '5/2/2013', 1 => '9:31:00 AM', 2 => '0.395', 3 => '0.395', 4 => '302.855', 5 => '0.563', ), 1 => array ( 0 => '5/2/2013', 1 => '9:33:00 AM', 2 => '0.383', 3 => '0.383', 4 => '303.431', 5 => '0.563', )

and for second array:

( 0 => array ( 0 => '5/2/2013', 1 => '9:31:00 AM', 2 => '-1.000', 3 => '-1.000', 4 => '-1.000', 5 => '-1.670', 6 => '-1.000', 7 => '-11.000', ), 1 => array ( 0 => '5/2/2013', 1 => '9:33:00 AM', 2 => '-1.000', 3 => '-1.000', 4 => '-1.000', 5 => '-1.670', 6 => '-1.000', 7 => '-11.000', )
  • 写回答

4条回答 默认 最新

  • douba5540 2013-07-19 07:34
    关注

    If both arrays are in the same order, the code is pretty straightforward:

    $a = array(
        array('5/2/2013', '9:31:00 AM', '0.395', '0.395', '302.855', '0.563'),
        array('5/2/2013', '9:33:00 AM', '0.383', '0.383', '303.431', '0.563'),
    );
    
    $b = array(
        array('5/2/2013', '9:31:00 AM', '-1.000', '-1.000', '-1.000', '-1.670', '-1.000', '-11.000'),
        array('5/2/2013', '9:33:00 AM', '-1.000', '-1.000', '-1.000', '-1.670', '-1.000', '-11.000'),
    );
    
    
    $i = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
    $i->attachIterator(new ArrayIterator($a), 'a');
    $i->attachIterator(new ArrayIterator($b), 'b');
    
    $result = [];
    foreach ($i as $v) {
        $result[] = array_merge($v['a'], array_slice($v['b'], 2));
    }
    print_r($result);
    

    You basically iterate over both arrays at the same time and for each element construct the final array by merging the first with the second (skipping the common part).

    Result:

    Array
    (
        [0] => Array
            (
                [0] => 5/2/2013
                [1] => 9:31:00 AM
                [2] => 0.395
                [3] => 0.395
                [4] => 302.855
                [5] => 0.563
                [6] => -1.000
                [7] => -1.000
                [8] => -1.000
                [9] => -1.670
                [10] => -1.000
                [11] => -11.000
            )
    
        [1] => Array
            (
                [0] => 5/2/2013
                [1] => 9:33:00 AM
                [2] => 0.383
                [3] => 0.383
                [4] => 303.431
                [5] => 0.563
                [6] => -1.000
                [7] => -1.000
                [8] => -1.000
                [9] => -1.670
                [10] => -1.000
                [11] => -11.000
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?