如何在php中从源数组中搜索特定的索引值数组

时间:2014-09-23 06:02:53

标签: php arrays

我有两个数组第一个源数组,其中存在静态5索引元素,第二个目标数组在于每个事务它变化的元素。所以我想通过从目标数组值中排序源数组来搜索最终结果数组。 我的演示代码是..

源数组 -

 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是合并。

1 个答案:

答案 0 :(得分: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);