如何在键的基础上组合多维数组

时间:2016-05-20 08:57:38

标签: php

Hi I have two arrays in my controller function following, I want final array with merged of     matched keys of both arrays to use further it as flowing:

//Array1
 Array
(
    [0] => Array
       (
            [LedgerID] => 573c321e6e3dcec3228b4567
            [LedgerName] => Purchase1
            [PanITNO] => ITR00123
            [dateOfPayment] => 08-Dec-2015
            [totalAmountPaid] => 9800
            [tdsRate] => 2
            [totalDudections] => 10000
        )

[1] => Array
    (
        [LedgerID] => 573befb96e3dceb6468b4567
        [LedgerName] => Development Fund
        [PanITNO] => dfdsfdsf
        [dateOfPayment] => 13-Jan-2016
        [totalAmountPaid] => 18000
        [tdsRate] => 10
        [totalDudections] => 20000
    )

)      // ARRAY2

    Array
   (
    [0] => Array
        (
           [Amountdeposited] => 200
           [dateOfDeposited] => 25-Feb-2016
           [challaNumber] => 1234
     )

    [1] => Array
        (
        [Amountdeposited] => 2000
        [dateOfDeposited] => 25-Feb-2016
        [challaNumber] => 1234
       )

) 
I want output like following Merged matched keys of both arrays into one final array:
array(
[0] => Array
   (
        [LedgerID] => 573c321e6e3dcec3228b4567
        [LedgerName] => Purchase1
        [PanITNO] => ITR00123
        [dateOfPayment] => 08-Dec-2015
        [totalAmountPaid] => 9800
        [tdsRate] => 2
        [totalDudections] => 10000,
        [Amountdeposited] => 200
        [dateOfDeposited] => 25-Feb-2016
        [challaNumber] => 1234

    )
[1] => Array
   (
        [LedgerID] => 573befb96e3dceb6468b4567
        [LedgerName] => Development Fund
        [PanITNO] => dfdsfdsf
        [dateOfPayment] => 13-Jan-2016
        [totalAmountPaid] => 18000
        [tdsRate] => 10
        [totalDudections] => 20000,
        [Amountdeposited] => 2000
        [dateOfDeposited] => 25-Feb-2016
        [challaNumber] => 1234

    )

) 如何组合两个数组的键以获得所需的结果, 如何组合两个数组的键以获得所需的结果, 如何组合两个数组的键以获得所需的结果, 如何组合两个数组的键以获得所需的结果, 如何组合两个数组的键以获得所需的结果, 如何组合两个数组的键以获得所需的结果,

先谢谢

2 个答案:

答案 0 :(得分:0)

您可以尝试此代码

 $i=0;
        $datas=array();
        foreach($array1 as $k=>$v)
        {
         $v['Amountdeposited'] = $array2[$i]['Amountdeposited'] ;
         $v['dateOfDeposited'] = $array2[$i]['dateOfDeposited'] ;
         $v['challaNumber']    = $array2[$i]['challaNumber'] ;
         $i++;
         $datas[$k]=$v;
        }

    print_r($datas);

答案 1 :(得分:0)

试试此代码

1)

 $out = array();
 foreach ($array1 as $key => $value){
      $out[] = array_merge((array)$value,(array)$array2[$key]);
 }
 print_r($out)

2)array_replace_recursive(仅当数组没有相同的键时才使用此函数将$ array key的值替换为$ array2键值)

print_r(array_replace_recursive($array1, $array2));