【问题标题】:php - how to merge 2d array that have different no of element for each arrayphp - 如何合并每个数组具有不同元素的二维数组
【发布时间】:2013-07-18 05:32:46
【问题描述】:

我有 2 组二维数组,我想合并到 1 个二维数组中。但是每个数组中的元素数量不同,前两个元素相同,我不想复制它。 在这里。

第一个二维数组:

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
           )
     )

第二个二维数组:

 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
            )
      )

我如何得到这样的结果:

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
            )
     )

这里是第一个数组的 var 导出:

( 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', )

对于第二个数组:

( 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', )

【问题讨论】:

    标签: php arrays merge multidimensional-array


    【解决方案1】:

    使用array_merge_recursive 示例

    $array = array_merge_recursive($array1, $array2);
     var_dump($array);
    

    【讨论】:

    • 如果@DevZer0 提示不起作用,您可以尝试自己进行合并。
    • 嗨@DevZer0,我已经尝试过,但它不像我想要的那样。因为如果我使用array_merge_recursive,它只会分别合并我的两个数组。我需要它合并而不重复前两个元素,即日期和时间。
    • @Zira 您可以使用自定义数组合并功能,然后您可以使用两个数组以我可以复制并粘贴到代码的格式更新您的帖子。
    【解决方案2】:

    如果两个数组的顺序相同,则代码非常简单:

    $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);
    

    您基本上同时遍历两个数组,并通过将第一个与第二个合并(跳过公共部分)为每个元素构造最终数组。

    结果:

    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
            )
    )
    

    【讨论】:

    • 嗨@Jack,我在实现你的代码时出错了。 array_slice() 期望参数 1 是数组,给定 null。
    • @zira 那么这意味着你的两个数组大小不相等?你能把你测试过的两个数组的var_export()粘贴到一起吗?
    • 是的,两个数组的大小不相同。我已经粘贴了上面测试过的两个数组的输出变量导出。
    • @zira 我已经尝试过这些数组,但它没有给出任何警告。你确定这些是正确的吗?
    【解决方案3】:

    这在很大程度上取决于您的实际目标是什么。例如,从您的示例来看,您似乎想使用日期 [index 0](可能还有时间 [index 1])作为各种“主键”来控制数据的合并方式。

    PHP 中没有合适的内置函数(如您所见,array_merge_recursive 不能满足您的要求):您必须自己动手。有很多“边缘情况”需要考虑。

    <?php
    /**
     * recursively merges each item in $a1 and $a2 if their indexes [0] and [1] match;
     * appends the $a2 item instead if no matching item is found in $a1.
     *
     * @param  array  $a1 the first array to merge
     * @param  array  $a2 the second array to merge
     * @return array  the merged array
     */
    function key_01_merge( array $a1,array $a2 ){
        // loop through each item in $a2
        foreach( $a2 as $a2_item ){
            // make sure it's an array with [0] and [1]
            if(
                ! is_array( $a2_item )
                || empty( $a2_item[0] )
                || empty( $a2_item[1] )
            ){
                // invalid; skip it
                break;
            }
            // compare it to each item in $a1; checking for matching "keys"
            foreach( $a1 as $a1_key => $a1_item ){
                if( 
                    ! empty( $a1_item[0] )
                    && ! empty( $a1_item[1] )
                    && $a1_item[0] === $a2_item[0] 
                    && $a1_item[1] === $a2_item[1] 
                ){
                    // merge the two arrays
                    // filter duplicate values
                    // assign resulting array to the original index in $a1
                    $a1[$a1_key] = array_unique( 
                        array_merge_recursive( $a1_item,$a2_item )
                    );
                    // set this item as "false" so it won't be appended to $a1
                    $a2_item = false;
                    // stop; continue with next item in $a2
                    break;
                }
            }
            // if $a2_item was merged, it is now false;
            // otherwise, append it to the $a1 array
            if( $a2_item ){
                $a1[] = $a2_item;
            }
        }
        // return the $a1 array
        return $a1;
    }
    

    经过测试。

    $a1 = [
        0 => [
            0 => '25/2/2013'
           ,1 => '8.45 a.m'
           ,2 => '9.98'
        ]
       ,1 => [
            0 => '25/2/2013'
           ,1 => '8.46 a.m'
           ,2 => '9.02'
        ]
    ];
    $a2 = [   
        0 => [
            0 => '25/2/2013'
           ,1 => '8.45 a.m'
           ,2 => '1.23'
           ,3 => '6.1'
        ]
       ,1 => [
            0 => '25/2/2013'
           ,1 => '8.46 a.m'
           ,2 => '1.75'
           ,3 => '3.5'
        ]
    ];
    
    var_dump( key_01_merge( $a1,$a2 ) );
    

    输出:

    /*
    array(2) {
      [0]=>
      array(5) {
        [0]=>
        string(9) "25/2/2013"
        [1]=>
        string(8) "8.45 a.m"
        [2]=>
        string(4) "9.98"
        [5]=>
        string(4) "1.23"
        [6]=>
        string(3) "6.1"
      }
      [1]=>
      array(5) {
        [0]=>
        string(9) "25/2/2013"
        [1]=>
        string(8) "8.46 a.m"
        [2]=>
        string(4) "9.02"
        [5]=>
        string(4) "1.75"
        [6]=>
        string(3) "3.5"
      }
    }
    */
    

    【讨论】:

    • tq @traq .its work.but,如果我对 $a2 中的第三个和第四个元素具有相同的值。 2=>'1.75', 3=>'1.75'.输出只显示新数组中的第四个元素。我尝试删除 array_unique,但不起作用。仅供参考,我只想日期和时间不重复。合并后其他元素保留。
    【解决方案4】:
    $out = array();
    for ($i=0; $i<count($arr1); $i++){
        $out[] = array_values(array_unique(array_merge($arr1[$i], $arr2[$i])));
    }
    var_dump($out);
    

    输出:

    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
            )
    
    )
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2018-09-20
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多