使用数组合并到一个foreach循环中 [英] using array merge into a foreach loop

查看:155
本文介绍了使用数组合并到一个foreach循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是我的循环:

  foreach($ doctor-> getVars()as $ k => $ v)
{
$ data [ '医生_'。 $ k] = $ v;


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



$ b $ p


$ b这里是$ data var_dump: $ b

  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:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b [0] =>数组

[CODE_] => 09HQ
[doctor_address1] =>备用addy
[doctor_address2] => 45854
[doctor_city] =>不同城市
[doctor_state] => CA
[doctor_zip] => 963545
[doctor_phone] => (619)111-2548
[doctor_fax] =>(157)123-4569



我是新的阵列合并,我假设正好在 $ data ['doctor_'之后。



$ b

语法是什么,我可以列出新的功能和我想合并的领域特别是? m不知道:

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

任何帮助将不胜感激,谢谢。


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

$ array_o 会现在包含所有元素 $ array_m



编辑:我只在你的文章中注意到你似乎想使用array_merge函数。您也可以执行以下操作:

pre $ array_o = array_merge($ array_o,array_m);


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.

解决方案

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

这篇关于使用数组合并到一个foreach循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆