想要使用php合并像array1 [0] + array2 [0]这样的数组

时间:2015-10-18 16:28:40

标签: php arrays

ARRAY1:

Array ( 
    [0] => Array ( 
        [name] => January 
        [scale_of_pay] => 7800-39000 
        [pay] => 25000 
        [grade_pay] => 5000 
        [d_a] => 5000 
        [h_r_a] => 5000 
        [t_a] => 1000 
        [other] => 5000 
        [net_claim] => 46000 
    )

数组2:

Array ( 
    [0] => Array ( 
        [national_two_bank] => 700 
        [bhgylaxshmi_banks] => 1000 
        [maheshwari_bank] => 1000 
        [maha_bank] => 1000 
        [mahesh_cooperative_bank] => 1000 
        [co_operative] => 1000 
        [lic] => 1000 
        [total_deduction] => 6700 
    ) 

想要答案如下:

Array ( 
    [0] => Array ( 
        [name] => January 
        [scale_of_pay] => 7800-39000 
        [pay] => 25000 
        [grade_pay] => 5000 
        [d_a] => 5000 
        [h_r_a] => 5000 
        [t_a] => 1000 
        [other] => 5000 
        [net_claim] => 46000 
        [national_two_bank] => 700 
        [bhgylaxshmi_banks] => 1000 
        [maheshwari_bank] => 1000 
        [maha_bank] => 1000 
        [mahesh_cooperative_bank] => 1000 
        [co_operative] => 1000 
        [lic] => 1000 
        [total_deduction] => 6700
    )

当我使用array_merge回答合并数组时,回答就像,

Array(
    [0] => Array(
        [name] => January[scale_of_pay] => 7800 - 39000[pay] => 25000[grade_pay] => 5000[d_a] => 5000[h_r_a] => 5000[t_a] => 1000[other] => 5000[net_claim] => 46000
    ) [1] => Array(
        [name] => February[scale_of_pay] => 7800 - 39000[pay] => 25000[grade_pay] => 5000[d_a] => 5000[h_r_a] => 5000[t_a] => 1000[other] => 5000[net_claim] => 46000
    ) [2] => Array(
        [name] => March[scale_of_pay] => 7800 - 39000[pay] => 25000[grade_pay] => 5000[d_a] => 5000[h_r_a] => 5000[t_a] => 5000[other] => 5000[net_claim] => 50000
    ) [3] => Array(
        [national_two_bank] => 700[bhgylaxshmi_banks] => 1000[maheshwari_bank] => 1000[maha_bank] => 1000[mahesh_cooperative_bank] => 1000[co_operative] => 1000[lic] => 1000[total_deduction] => 6700
    ) [4] => Array(
        [national_two_bank] => 1000[bhgylaxshmi_banks] => 1000[maheshwari_bank] => 1000[maha_bank] => 1000[mahesh_cooperative_bank] => 1000[co_operative] => 1000[lic] => 1000[total_deduction] => 7000
    ) [5] => Array(
        [national_two_bank] => 1000[bhgylaxshmi_banks] => 1000[maheshwari_bank] => 1000[maha_bank] => 1000[mahesh_cooperative_bank] => 1000[co_operative] => 1000[lic] => 1000[total_deduction] => 7000
    )
)

我不想这样......

2 个答案:

答案 0 :(得分:0)

您想使用array_merge功能。在您的具体示例中,它就像array_merge($array1[0], $array2[0])

一样简单

这是一个例子

<?php
    $array1 = array( array( 'test' => 'testing1', 'test2' => 'testing2' ) );
    $array2 = array( array( 'test3' => 'testing3', 'test4' => 'testing4' ) );
    print_r( array_merge( $array1[0], $array2[0] ) );
?>

此输出

  

数组([test] =&gt; testing1 [test2] =&gt; testing2 [test3] =&gt; testing3   [test4] =&gt; testing4)

答案 1 :(得分:0)

对于适用于具有多个内部值的数组的解决方案,请尝试:

array_map('array_merge', $a1, $a2);

以下是https://repl.it/BRdQ

的示例