使用数组合并到foreach循环中

时间:2013-05-08 17:44:30

标签: php foreach array-merge

如果他们在个人资料中有替代信息,我需要将新的替代信息数组合并到循环中。

这是我的循环:

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

这里是$ 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
)

我需要将下面的数组合并到上面的数组中。 这是函数addr($ dfcode)的print var:

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
        )

)

我是数组合并的新手,我在$data['doctor_'. $k] = $v我可以列出新功能以及我想特别合并的字段后立即假设?

语法是我不确定的:

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

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:8)

合并两个数组的通用公式如下(将$ array_m合并到$ array_o中):

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

$array_o现在将包含$array_m

的所有元素 编辑:我刚刚在你的帖子中注意到你似乎想要使用array_merge函数。您还可以执行以下操作:

$array_o = array_merge($array_o, array_m);