Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment (requires an account with 50 reputation)

4 Answers

up vote 1 down vote accepted

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
        )
)
share|improve this answer
Hi @Jack, i got error when implement your code. array_slice() expect parameter 1 to be array, null given. – zira Jul 19 at 7:45
@zira Then that means your two arrays are not of equal size? Could you pastebin a var_export() of both arrays you tested with? – Jack Jul 19 at 7:56
yes, the size of two arrays not same.i have paste the output var export for both array that i'm tested above. – zira Jul 19 at 8:18
@zira I've tried those arrays and it doesn't give any warnings. Are you sure those are the correct ones? – Jack Jul 19 at 8:28
its work now.tq so much! – zira Jul 22 at 2:18
add comment (requires an account with 50 reputation)
$out = array();
for ($i=0; $i<count($arr1); $i++){
    $out[] = array_values(array_unique(array_merge($arr1[$i], $arr2[$i])));
}
var_dump($out);

Output:

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
        )

)
share|improve this answer
add comment (requires an account with 50 reputation)

It depends a lot on what your actual goal is. For instance, from your example, it seems that you want to use the date [index 0] (and possibly the time [index 1]) as a "primary key" of sorts, to control how data is merged.

There is no suitable function for this built in to PHP (as you've noticed, array_merge_recursive doesn't do what you want): you'll have to roll your own. There are a lot of "edge cases" to consider.

<?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;
}

tested.

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

outputs:

/*
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"
  }
}
*/
share|improve this answer
tq @traq .its work.but, how if i have same value for third and fourth element in $a2. 2=>'1.75', 3=>'1.75'.the output only show fourth element in new array.i have tried remove array_unique,but does not work. fyi, i only want to date and time not duplicate.other element remain after merging. – zira Jul 19 at 7:07
add comment (requires an account with 50 reputation)

use array_merge_recursive example

$array = array_merge_recursive($array1, $array2);
 var_dump($array);
share|improve this answer
If the @DevZer0 tip, dont work, yo can try do the merge yourself. – ramiromd Jul 19 at 3:14
Hi @DevZer0, i have tried but its not like what i want.because if i'm using array_merge_recursive, its only merge my two array respectively. i need it merging without duplicate the first two element which are date and time. – zira Jul 19 at 3:35
@Zira you can use a custom array merge function then, can you update your post with the two arrays in a format i can copy and paste to code. – DevZer0 Jul 19 at 3:47
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.