2016-05-26 12 views
-2

次の2つの配列が動的に成長しています。キーに基づいて個々の配列をマージする

まず配列

Array 
    (
    [Development Fund] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 01-Dec-2015 
        [totalAmountPaid] => 49000 
        [tdsRate] => 2 
        [totalDudections] => 980 
       ) 

      [1] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 01-Mar-2016 
        [totalAmountPaid] => 4000 
        [tdsRate] => 2 
        [totalDudections] => 80 
       ) 

      [2] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 23-Mar-2016 
        [totalAmountPaid] => 19000 
        [tdsRate] => 2 
        [totalDudections] => 380 
       ) 

     ) 

    [Registration Fee] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 5739b50b6e3dce450e8b4567 
        [LedgerName] => Registration Fee 
        [PanITNO] => ITR0
        [dateOfPayment] => 02-Jan-2016 
        [totalAmountPaid] => 1000 
        [tdsRate] => 10 
        [totalDudections] => 100 
       ) 

     ) 

    [Purchase1] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 573c321e6e3dcec3228b4567 
        [LedgerName] => Purchase1 
        [PanITNO] => ITR0
        [dateOfPayment] => 02-Jan-2016 
        [totalAmountPaid] => 8000 
        [tdsRate] => 2 
        [totalDudections] => 160 
       ) 

     ) 

) 

二番目の配列

Array 
(
    [Development Fund] => Array 
    (
     [0] => Array 
      (     
       [Amountdeposited] => 1000 
       [dateOfDeposited] => 01-Jan-2016 
       [challaNumber] => 2 
      ) 

     [1] => Array 
      (     
       [Amountdeposited] => 50000000 
       [dateOfDeposited] => 03-Mar-2016 
       [challaNumber] => 5555 
      ) 

     [2] => Array 
      (     
       [Amountdeposited] => 90000 
       [dateOfDeposited] => 24-Mar-2016 
       [challaNumber] => 999 
      ) 

    ) 

私は、出力等がキーに基づいて、次の必要があります。

決勝配列

Array 
    (
    [Development Fund] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 01-Dec-2015 
        [totalAmountPaid] => 49000 
        [tdsRate] => 2 
        [totalDudections] => 980 
        [Amountdeposited] => 1000 
        [dateOfDeposited] => 01-Jan-2016 
        [challaNumber] => 2 
       ) 

      [1] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 01-Mar-2016 
        [totalAmountPaid] => 4000 
        [tdsRate] => 2 
        [totalDudections] => 80 
        [dateOfDeposited] => 03-Mar-2016 
        [challaNumber] => 5555 
       ) 

      [2] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 23-Mar-2016 
        [totalAmountPaid] => 19000 
        [tdsRate] => 2 
        [totalDudections] => 380 
        [Amountdeposited] => 90000 
        [dateOfDeposited] => 24-Mar-2016 
        [challaNumber] => 999 
       ) 

     ) 

    [Registration Fee] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 5739b50b6e3dce450e8b4567 
        [LedgerName] => Registration Fee 
        [PanITNO] => ITR0
        [dateOfPayment] => 02-Jan-2016 
        [totalAmountPaid] => 1000 
        [tdsRate] => 10 
        [totalDudections] => 100 
       ) 

     ) 

    [Purchase1] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 573c321e6e3dcec3228b4567 
        [LedgerName] => Purchase1 
        [PanITNO] => ITR0
        [dateOfPayment] => 02-Jan-2016 
        [totalAmountPaid] => 8000 
        [tdsRate] => 2 
        [totalDudections] => 160 
       ) 

     ) 

) 

それがどのPHPの組み込み関数を使用して行うことが可能かどう示唆してください。

+0

ウムお勧めしてください」繰り返す意味した任意のPHP直接の関数も可能です私のための緊急の。誰もそれが大好きだとは思わない。私は誰かがあなたの質問をd​​ownvotes場合は驚かないだろう。 – theruss

答えて

0

このコードを試してください。配列を連結するには、$arr1+$arr2が動作します。しかし、それはあなたのケースでは動作していません。 Array_merge、再帰的なマージ、組み合わせはあなたのケースでは動作していません。ですから、ループをマージするためにforループを使用してみてください。

foreach ($arr1 as $key1 => $value1) { 
    if($key1 == 'Development Fund') { 
     foreach($arr2['Development Fund'] as $key2 => $value2){ 
     array_push($arr1['Development Fund'][$key2],$value2); 
    } 
    } 
} 

しかし、これはあなたがこの

Array 
(
    [Development Fund] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 01-Dec-2015 
        [totalAmountPaid] => 49000 
        [tdsRate] => 2 
        [totalDudections] => 980 
        [0] => Array 
         (
          [Amountdeposited] => 1000 
          [dateOfDeposited] => 01-Jan-2016 
          [challaNumber] => 2 
         ) 

       ) 

      [1] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 01-Mar-2016 
        [totalAmountPaid] => 4000 
        [tdsRate] => 2 
        [totalDudections] => 80 
        [0] => Array 
         (
          [Amountdeposited] => 50000000 
          [dateOfDeposited] => 03-Mar-2016 
          [challaNumber] => 5555 
         ) 

       ) 

      [2] => Array 
       (
        [LedgerID] => 573befb96e3dceb6468b4567 
        [LedgerName] => Development Fund 
        [PanITNO] => UK07AK1787 
        [dateOfPayment] => 23-Mar-2016 
        [totalAmountPaid] => 19000 
        [tdsRate] => 2 
        [totalDudections] => 380 
        [0] => Array 
         (
          [Amountdeposited] => 90000 
          [dateOfDeposited] => 24-Mar-2016 
          [challaNumber] => 999 
         ) 

       ) 

     ) 

    [Registration Fee] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 5739b50b6e3dce450e8b4567 
        [LedgerName] => Registration Fee 
        [PanITNO] => ITR0
        [dateOfPayment] => 02-Jan-2016 
        [totalAmountPaid] => 1000 
        [tdsRate] => 10 
        [totalDudections] => 100 
       ) 

     ) 

    [Purchase1] => Array 
     (
      [0] => Array 
       (
        [LedgerID] => 573c321e6e3dcec3228b4567 
        [LedgerName] => Purchase1 
        [PanITNO] => ITR0
        [dateOfPayment] => 02-Jan-2016 
        [totalAmountPaid] => 8000 
        [tdsRate] => 2 
        [totalDudections] => 160 
       ) 

     ) 

) 

のような結果となりますが、あなたのためのそのOKですか?

0
function MergMultiDmsAry($array1,$array2) 
{ 

$i = 0; 
$newa = array(); 
$god = array(); 
$final = array(); 

foreach($array1 as $key=>$value) 
{ 
    for($j = 0; $j < count($value); $j++) 
    { 
     $newa = array_merge($value[$j],$array2[$key][$j]); 
     $god[] = $newa; 

} 

$i++; 

    // set the key 
    $final[$key] = $god;  
} 


print_r($final); 

}

$data = array('dev'=>array(array('id'=>1),array('id'=>2))); 
$data2 =  array('dev'=>array(array('name'=>'john','address'=>'damak'),array('name'=>'mayer','address'=>'kathmandu'))); 

MergMultiDmsAry($data,$data2); 

この意志はあなたに

+0

ここに2つの答えがあります。あなたの答えを編集し、最初に2番目の答えを追加することができます。 –

0

を助けるかもしれない、私は答える与えた:

Array ([dev] => Array 
     ([0] => 
      Array ([id] => 1 [name] => john [address] => damak) 
     [1] => 
     Array ([id] => 2 [name] => mayer [address] => kathmandu) 
)) 
関連する問題