tags:

views:

33

answers:

2

Not sure how to go about this...

But, I have two arrays, one with updated information, another with outdated information... There are a lot more elements in the second array, but I'm looking to "update" the outdated one with the updated information.

Here's what the arrays look like:

//Outdated
Array (
    [0] => Array
        (
            [anum] => 3236468462
            [cid] => 4899097762
            [mid] => 1104881401  
            [na_title] =>         
            [na_fname] => JOHN                   
            [m_initial] =>  
            [na_lname] => DOE           
            [na_suffix] =>     
            [na_addr1] => 1234 SAMPLE AVENUE           
            [na_addr2] =>                             
            [na_city] => NORWALK           
            [state] => OH
            [zip] => 
            [zip_plus_4] => 
            [route] => R002
            [dma_code] => 510334
        )
)


//Updated
Array (
    [1] => Array
        (
            [0] => YUD990
            [1] => 98
            [2] => 1234 Sample Avenue
            [3] => 
            [4] => Norwalk
            [5] => OH
            [6] => 44857-9215
            [7] => 3236468462
        )
)

To clarify, I want to:

(1) Match up the value for [7] from the updated array with the value for [anum] in the outdated array, and then update [na_addr1], [na_addr2], [na_city], [state], [zip], [zip_plus_4] in the outdated array with the values for [2],[3],[4],[5],[6] (I know I'll need to split the updated [6] in order to get it to map corrected to the outdated)

Feel like I'm making this very confusing... sorry about that...

+1  A: 

Your updated array needs to have matching keys, otherwise there's no way to know how the values should replace the old ones. Then the standard array merge works.

$new = array_merge($outdated, $updated);
Tesserex
+1  A: 

Assuming that the structure of the update array will never change, you could just use the code below. (I'm assuming that 'zip' is the first 5 digits and zip_plus_4 is the last 4 digits, but I'm not clear exactly what they're supposed to be.)

$old_array['anum'] = $new_array[7];
$old_array['na_addr1'] = $new_array[2];
$old_array['na_addr2'] = $new_array[2];
$old_array['na_city'] = $new_array[3];
$old_array['state'] = $new_array[4];
$zip_code = explode('-', $new_array[6]);
$old_array['zip'] = $zip_code[0];
$old_array['zip_plus_4'] = $zip_code[1];

I'm not sure why the second array doesn't use its own set of matching keys. It would be more readable and help keeps things consistent. For example, if you end up adding another field, some of the elements could be offset by one, which would just cause headaches. But if the arrays used the same keys, you could use the code below and everything would be fine.

$old_array['anum'] = $new_array['anum'];
$old_array['na_addr1'] = $new_array['na_addr1'];
(etc)
Dazarath
Also, if the keys were matched, you could simply add them. $old_array = $new_array + $old_array;When keys conflict, the one in the first array (on the left of the + operator) will be kept.
Daniel