-1

Need help from the team,

I have this scenario of having 2 identical keys in each array with different values, i want them to be merged into one key were the values also are in it

example:

arrayData1(
 [2] => Array
        (
            [EXP1] => Array (records...)
            [EXP2] => Array (records...)
        )
)

arrayData2(
 [2] => Array
        (
            [EXP3] => Array (records...)
            [EXP4] => Array (records...)
        )
)

Having the output like this:

arrayFinal (
 [2] => Array
       (
           [EXP1] => Array (records...)
           [EXP2] => Array (records...)
           [EXP3] => Array (records...)
           [EXP3] => Array (records...)
       )
)

Thanks!

danteboi
  • 9
  • 4
  • array_merge_recursive() might do it – ka_lin Mar 26 '16 at 09:19
  • 1
    possible duplicate of `http://stackoverflow.com/questions/1558291/php-merge-2-multidimensional-arrays` – santosh Mar 26 '16 at 09:27
  • ive tried but its no luck...arrayFinal = array_merge_recursive(arrayData1, arrayData2); – danteboi Mar 26 '16 at 09:28
  • Use array_merge_recursive() as suggested above, that will append it to the array value of the key..and also you cannot have two same keys in a single array..that won't make sense – Vishnu Nair Mar 26 '16 at 09:30

1 Answers1

0

First of all you cannot have two same keys in a single array, what you can do is use the array_merge_recursive function in php to merge both the arrays, and the repeating keys will have a new array with all the repeating key values..

$array1 = [
'EXP1' => [1,2,3],
'EXP2' => [2,3,4] 
];

$array2 = [
'EXP2' => [5,6,7],
'EXP3' => [8,9,10] 
];

Now there are two EXP2 keys, so when you use array_merge_recursive() you get something like this,

print_r(array_merge_recursive($array1, $array2));
//output Array (
[EXP1] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
    )

[EXP2] => Array
    (
        [0] => 2
        [1] => 3
        [2] => 4
        [3] => 5
        [4] => 6
        [5] => 7
    )

[EXP3] => Array
    (
        [0] => 8
        [1] => 9
        [2] => 10
    )

)

Vishnu Nair
  • 2,395
  • 14
  • 16
  • well currently that is the scenario..having the same key from a diff set of array datas..thanks anyway vince – danteboi Mar 26 '16 at 10:20