php中的数组比较出错

时间:2015-05-27 07:21:55

标签: php arrays zend-framework foreach zend-framework2

以下是我正在比较的示例数组。

表1

 $csvData=
 (
 [0] => Array
    (
        [Account Number] => 1.01100156278101E+15
        [Posting Date] => 2014/07/08
        [Value Date] => 2014/07/08
        [Description] => Cash Withdrawal by Cheque-173320--TT1421957901
        [Debit Amount] => 2000
        [Credit Amount] => 
        [Running Balance] => 388392.62
    )

[1] => Array
    (
        [Account Number] => 1.01100156278101E+15
        [Posting Date] => 2014/07/08
        [Value Date] => 2014/07/08
        [Description] => Cheque Paid-173261--TT1421951241
        [Debit Amount] => 855
        [Credit Amount] => 
        [Running Balance] => 390392.62
    )

[2] => Array
    (
        [Account Number] => 1.01100156278101E+15
        [Posting Date] => 2014/07/08
        [Value Date] => 2014/07/08
        [Description] => Cheque Paid-173298--TT1421951226
        [Debit Amount] => 1895
        [Credit Amount] => 
        [Running Balance] => 391247.62
    )

[3] => Array
    (
        [Account Number] => 1.01100156278101E+15
        [Posting Date] => 2014/06/08
        [Value Date] => 2014/06/08
        [Description] => Cash Withdrawal by Cheque-173319--TT1421858465
        [Debit Amount] => 2750
        [Credit Amount] => 
        [Running Balance] => 393142.62
    )

)

表2

 $dbData=
        (
    [12] => Array
    (
        [JournalReferenceId] => 12
        [DebitAmount] => 3500.00
        [CreditAmount] => 0.00
        [Description] => 
        [JournalReferenceCreatedDate] => 2015-04-27 15:51:53
        [JournalId] => 4
        [ChequeNumber] => 
        [ChequeDate] => 
        [AccountNumber] => 1.01100156278101E+15
        [bankId] => 15
        [BankReconcileId] => 
        [IsPresent] => 
        [CreatedDate] => 
        [CreatedBy] => 
        [JournalApprovedBy] => 
    )

[20] => Array
    (
        [JournalReferenceId] => 20
        [DebitAmount] => 350.00
        [CreditAmount] => 0.00
        [Description] => 
        [JournalReferenceCreatedDate] => 2015-04-27 15:53:42
        [JournalId] => 6
        [ChequeNumber] => 
        [ChequeDate] => 
        [AccountNumber] => 1.01100156278101E+15
        [bankId] => 15
        [BankReconcileId] => 
        [IsPresent] => 
        [CreatedDate] => 
        [CreatedBy] => 
        [JournalApprovedBy] => 
    )

[56] => Array
    (
        [JournalReferenceId] => 56
        [DebitAmount] => 26.40
        [CreditAmount] => 0.00
        [Description] => Initial Amount
        [JournalReferenceCreatedDate] => 2015-04-27 16:40:35
        [JournalId] => 18
        [ChequeNumber] => 
        [ChequeDate] => 
        [AccountNumber] => 1.01100156278101E+15
        [bankId] => 15
        [BankReconcileId] => 
        [IsPresent] => 
        [CreatedDate] => 
        [CreatedBy] => 
        [JournalApprovedBy] => 
    )

[68] => Array
    (
        [JournalReferenceId] => 68
        [DebitAmount] => 20.66
        [CreditAmount] => 0.00
        [Description] => Initial Amount
        [JournalReferenceCreatedDate] => 2015-04-27 16:49:33
        [JournalId] => 21
        [ChequeNumber] => 
        [ChequeDate] => 
        [AccountNumber] => 1.01100156278101E+15
        [bankId] => 15
        [BankReconcileId] => 
        [IsPresent] => 
        [CreatedDate] => 
        [CreatedBy] => 
        [JournalApprovedBy] => 
    )

[88] => Array
    (
        [JournalReferenceId] => 88
        [DebitAmount] => 3332112.00
        [CreditAmount] => 0.00
        [Description] => 
        [JournalReferenceCreatedDate] => 2015-05-05 18:29:47
        [JournalId] => 28
        [ChequeNumber] => 
        [ChequeDate] => 
        [AccountNumber] => 1.01100156278101E+15
        [bankId] => 15
        [BankReconcileId] => 
        [IsPresent] => 
        [CreatedDate] => 
        [CreatedBy] => 
        [JournalApprovedBy] => 1
    )

[136] => Array
    (
        [JournalReferenceId] => 136
        [DebitAmount] => 359.14
        [CreditAmount] => 0.00
        [Description] => Initial Amount
        [JournalReferenceCreatedDate] => 2015-05-13 15:50:51
        [JournalId] => 42
        [ChequeNumber] => 
        [ChequeDate] => 
        [AccountNumber] => 1.01100156278101E+15
        [bankId] => 15
        [BankReconcileId] => 
        [IsPresent] => 
        [CreatedDate] => 
        [CreatedBy] => 
        [JournalApprovedBy] => 
    )

代码

   foreach ($dbData as $dbRow) {


        $csv_ied_account_number = ($this->_convertDbValueToCsvLikeValue($this->_csvColumnAccountNumber, $dbRow));

        $datetime = ($this->_convertDbValueToCsvLikeValue($this->_csvColumnJournalDate, $dbRow));
        $dt = new DateTime($datetime);

        $csv_ied_journal_date = $dt->format('m/d/Y');

        $csv_ied_debit_amount = ($this->_convertDbValueToCsvLikeValue($this->_csvColumnDebitAmount, $dbRow));

        $csv_ied_credit_amount = ($this->_convertDbValueToCsvLikeValue($this->_csvColumnCreditAmount, $dbRow)); 

  /*Compare with csv and db data*/ 

       $CsvValues = array();     
        foreach ($csvData as $key => $csvRow) {


       if ((($csvRow[$this->_csvColumnAccountNumber]) === $csv_ied_account_number) &&
                    (($csvRow[$this->_csvColumnJournalDate]) === $csv_ied_journal_date) ) {

                 $CsvValues[$key]= 1;  
            }
        }


        if (count($CsvValues) > 0) {
            asort($CsvValues);
            $key = key($CsvValues); 
            $dbRow['IsPresent'] = 1;
            unset($csvData[$key]);
        }

注意 表头已经全局声明。所以没问题。

我使用的代码只是比较第一行,但它应该将整行与每个数据库行进行比较并传递** $ dbRow ['IsPresent'] = 1 **对应于该行排。请帮帮我吧。我一直花很多时间在这上面。谢谢!

1 个答案:

答案 0 :(得分:0)

只需使用

即可解决问题
(($csvRow[$this->_csvColumnJournalDate]) == $csv_ied_journal_date) )

而不是

 (($csvRow[$this->_csvColumnJournalDate]) === $csv_ied_journal_date) )

现在就像一个魅力!

对于相同的值

==

=== 相同的数据类型和值