using array merge into a foreach loop
Asked Answered
M

1

6

I need to merge a new array of alternative information into the loop if they have the alternative information in their profile.

Here's my loop:

foreach ($doctor->getVars() as $k => $v)
    {
    $data['doctor_'. $k] = $v;
    }

foreach ($patient->get_data() as $k=>$v)
    {
    if (is_string($v) || is_numeric($v))
        $data["patient_" . $k] = strtoupper($v);
    } 

Here's the $data var_dump:

Array
(
    [employee] => person
    [date] => 05/08/2013
    [datetime] => 05/08/2013 9:41:15 AM
    [department] => stuff
    [employee_ext] => 7457
    [employee_email] => 
    [barcode] => *NZS01*
    [doctor_df_code] => 09HQ
    [doctor_npi] => 1111111111
    [doctor_dea] => B4574
    [doctor_upin] => 
    [doctor_license] => 
    [doctor_phone] => (111)111-1111
    [doctor_fax] => (000)000-0000
    [doctor_fname] => UNDEFINED
    [doctor_lname] => UNDEFINED
    [doctor_title] => 
    [doctor_intake_rx_caller_id] => 
    [doctor_costco_rx_caller_id] => 
    [doctor_reorder_rx_caller_id] => 
    [doctor_address1] => 24 CABELL st
    [doctor_address2] => SUITE 10
    [doctor_city] => places
    [doctor_state] => CA
    [doctor_zip] => 91111
    [doctor_active_events] => 
    [doctor_dont_call] => 0
    [doctor_dont_fax] => 1
)

I need to merge the below array into the above array. Here's the print var for the function addr($dfcode):

Array
(
    [0] => Array
        (
            [CODE_] => 09HQ
            [doctor_address1] => alternate addy
            [doctor_address2] => 45854
            [doctor_city] => different city
            [doctor_state] => CA
            [doctor_zip] => 963545
            [doctor_phone] => (619)111-2548
            [doctor_fax] => (157)123-4569
        )

)

I'm new to array merge and I'm assuming right after the $data['doctor_'. $k] = $v i could list out the new function and the fields i want to merge in particular?

syntax is what i'm not sure on:

$data['doctor_'. $k] . array_merge(addr($dfcode))['doctor_address1'] = $v;

Any help would be greatly appreciated, thank you.

Midkiff answered 8/5, 2013 at 17:44 Comment(8)
foreach($array_bot as $key=>$value){ $array_top[$key]=$value; } this will merge the 2 arrays i think. Didnt test the code. But you need to have both arrays from the begin. Dont know if this is your true questionAdorn
When you say you want to merge this information IF they have alternative information, do you mean that you want to merge the data from the second array only if the second array contains the information? I'm not completely clear on what your conditions are here. Also, all but one of the fields in the second array are duplicates of fields in the first array. Are you replacing data in the original array? That would be different than merging.Georgena
Yes, merge the data from the second array only if the second array contains the information. Replacing data in the original array.Midkiff
Are you assuming that the keys will exist, but that they might not contain any values? Or do you need to also check to see if the keys exist?Georgena
They will always exist and have maybe not always have values.Midkiff
Then I'm horribly confused at what you need to check for. You keep referencing "the information" in your posts, what do you mean by "the information"?Georgena
Just overwriting the first array (information) with the second array (information) if there is data in the second array, there will always be keys and values in the first array but not always in the second. Hope that makes more sense.Midkiff
Then the answer I posted below using the foreach loop is exactly what you're looking for. If the keys don't exist in the second array, the data in the first array won't be overwritten. Otherwise it will.Georgena
G
14

The general formula for merging two arrays is as follows (merging $array_m into $array_o):

foreach($array_m as $key=>$value){ 
    $array_o[$key] = $value;
}

$array_o would now contain all of the elements of $array_m

EDIT: I just noticed in your post that you seem to want to use the array_merge function. You could also do the following:

$array_o = array_merge($array_o, array_m);
Georgena answered 8/5, 2013 at 18:2 Comment(2)
yes, the array_merge function is what i was thinking would get the job done. I'm not sure how to translate $array_o and array_m into my loop though.Midkiff
$data = array_merge($data['doctor_'. $k],addr($dfcode)) = $v;Midkiff

© 2022 - 2024 — McMap. All rights reserved.