2014-09-23 148 views
0

我有兩個數組第一個源數組,其中存在靜態5索引元素,第二個目標數組中的每個事務都會變化元素。所以我想通過從目標數組值中排序源數組來搜索最終結果數組。 我的演示代碼是..如何從源數組中搜索特定的索引值數組php

源陣列 -

Array 
    (
     [0] => Array 
      (
       [SRNO] => 6 
       [Particulars1] => Add : Company Un-Reconciled Receipts (Cheque deposited but not cleared) 
       [Particulars] => Cash Received 
       [TXNName] => REC2209141 
       [TXNDate] => 2014-09-22 
       [Branch] => Nagpur 
       [BranchID] => 156 
       [AccountID] => 959 
       [Account] => sbi 
       [ContactID] => 948 
       [Contact] => 0 
       [Narration] => Cash Received 
       [BankID] => 4812 
       [BankName] => A B E CO-OP. BANK LTD. 
       [InstrumentType] => 560 
       [InstrumentTypeName] => 
       [InstrumentNumber] => 123456 
       [InstrumentDate] => 2014-09-22 
       [Amount] => 15000.000 
       [ID] => 1 
       [GroupID] => 2 
      ) 

     [1] => Array 
      (
       [SRNO] => 6 
       [Particulars1] => Add : Company Un-Reconciled Receipts (Cheque deposited but not cleared) 
       [Particulars] => Cash Received 
       [TXNName] => REC2209142 
       [TXNDate] => 2014-09-22 
       [Branch] => Nagpur 
       [BranchID] => 156 
       [AccountID] => 959 
       [Account] => sbi 
       [ContactID] => 948 
       [Contact] => 0 
       [Narration] => Cash Received 
       [BankID] => 4812 
       [BankName] => A B E CO-OP. BANK LTD. 
       [InstrumentType] => 560 
       [InstrumentTypeName] => 
       [InstrumentNumber] => 123456 
       [InstrumentDate] => 2014-09-22 
       [Amount] => 1500.000 
       [ID] => 2 
       [GroupID] => 2 
      ) 
    ) 

目的地陣列 -

Array 
(

    [0] => Array 
     (
      [SRNO] => 1 
      [Particulars] => Bank Pass Book balance 
      [TXNName] => 
      [TXNDate] => 
      [Branch] => 
      [BranchID] => 0 
      [AccountID] => 0 
      [Account] => 
      [ContactID] => 0 
      [Contact] => 
      [Narration] => 
      [BankID] => 0 
      [BankName] => 
      [InstrumentType] => 
      [InstrumentTypeName] => 
      [InstrumentNumber] => 
      [InstrumentDate] => 
      [Amount] => 1500 
      [ID] => -1 
      [GroupID] => 0 
     ) 

    [1] => Array 
     (
      [SRNO] => 2 
      [Particulars] => Less : Bank Un-reconciled Payments (Debited by bank but not accounted) 
      [TXNName] => 
      [TXNDate] => 
      [Branch] => 
      [BranchID] => 0 
      [AccountID] => 0 
      [Account] => 
      [ContactID] => 0 
      [Contact] => 
      [Narration] => 
      [BankID] => 0 
      [BankName] => 
      [InstrumentType] => 
      [InstrumentTypeName] => 
      [InstrumentNumber] => 
      [InstrumentDate] => 
      [Amount] => 0 
      [ID] => 3 
      [GroupID] => 0 
     ) 

    [2] => Array 
     (
      [SRNO] => 3 
      [Particulars] => Add : Bank Un-reconciled Receipts (Credited by bank but not accounted 
      [TXNName] => 
      [TXNDate] => 
      [Branch] => 
      [BranchID] => 0 
      [AccountID] => 0 
      [Account] => 
      [ContactID] => 0 
      [Contact] => 
      [Narration] => 
      [BankID] => 0 
      [BankName] => 
      [InstrumentType] => 
      [InstrumentTypeName] => 
      [InstrumentNumber] => 
      [InstrumentDate] => 
      [Amount] => 0 
      [ID] => 4 
      [GroupID] => 0 
     ) 

    [3] => Array 
     (
      [SRNO] => 4 
      [Particulars] => Company Pass Book balance 
      [TXNName] => 
      [TXNDate] => 
      [Branch] => 
      [BranchID] => 0 
      [AccountID] => 0 
      [Account] => 
      [ContactID] => 0 
      [Contact] => 
      [Narration] => 
      [BankID] => 0 
      [BankName] => 
      [InstrumentType] => 
      [InstrumentTypeName] => 
      [InstrumentNumber] => 
      [InstrumentDate] => 
      [Amount] => 1500 
      [ID] => -2 
      [GroupID] => 0 
     ) 

    [4] => Array 
     (
      [SRNO] => 5 
      [Particulars] => Less : Company Un-Reconciled Payments (Cheque issued but not presented) 
      [TXNName] => 
      [TXNDate] => 
      [Branch] => 
      [BranchID] => 0 
      [AccountID] => 0 
      [Account] => 
      [ContactID] => 0 
      [Contact] => 
      [Narration] => 
      [BankID] => 0 
      [BankName] => 
      [InstrumentType] => 
      [InstrumentTypeName] => 
      [InstrumentNumber] => 
      [InstrumentDate] => 
      [Amount] => 0 
      [ID] => 1 
      [GroupID] => 0 
     ) 

    [5] => Array 
     (
      [SRNO] => 6 
      [Particulars] => Add : Company Un-Reconciled Receipts (Cheque deposited but not cleared) 
      [TXNName] => 
      [TXNDate] => 
      [Branch] => 
      [BranchID] => 0 
      [AccountID] => 0 
      [Account] => 
      [ContactID] => 0 
      [Contact] => 
      [Narration] => 
      [BankID] => 0 
      [BankName] => 
      [InstrumentType] => 
      [InstrumentTypeName] => 
      [InstrumentNumber] => 
      [InstrumentDate] => 
      [Amount] => 0 
      [ID] => 2 
      [GroupID] => 0 
     ) 

) 

排序第一陣列SRNO -6從目的地數組排序兩者合併後。

+0

你的問題還不清楚。什麼是期望的輸出? – cartina 2014-09-23 06:07:30

+0

你好先生,我想從SRNO第一到第二的搜索數組。 – Abhishek 2014-09-23 06:08:55

+0

請快速回復。 – Abhishek 2014-09-23 06:37:48

回答

0
// First find all SRNOs from destination array 
$key = 'SRNO'; 

$output = array_map(function($item) use ($key) { 
    return $item[$key]; 
}, $destArray); 


print_r($output); 

//Find matching SRNOs from first array and store in another array(final) 


$key = 'SRNO'; 

$output = array_map(function($item) use ($key) { 
    return $item[$key]; 
}, $testArray); 


//print_r($output); 

$final = array(); 

foreach($sourceArray as $row){ 
    if(in_array($row['SRNO'],$output)){ 
       $final[]=$row; 
    } 
} 

//This will output array with matching SRNOs from destination array 
print_r($final);