简体   繁体   English

两个数组,一个有索引,一个没有 - 如何测试

[英]Two arrays, one with index one without - how to test

I have two arrays.我有两个数组。 One array is a single record array with no index - it is how all single records are provided by the vendor.一个数组是一个没有索引的单个记录数组——这是供应商提供所有单个记录的方式。 The second array (having two or more records contains indexes [0] - [9]. I have a foreach loop which works great on the multiple records but the foreach does not work on the single record (which may be a string). I need a solution to test the difference between these two and the best option appears to be the index itself. Let me know if you see it differently.第二个数组(有两个或多个记录包含索引 [0] - [9]。我有一个 foreach 循环,它在多个记录上工作得很好,但 foreach 对单个记录(可能是一个字符串)不起作用)。我需要一个解决方案来测试这两者之间的差异,最好的选择似乎是索引本身。如果您有不同的看法,请告诉我。

Here is the array key (it is the same in every call): Array ( [0] => 0 )这是数组键(每次调用都相同): Array ( [0] => 0 )

Here is the single record result:以下是单条记录结果:

Array ( [DepositsGetInfoResult] => Array ( [DepositObject] => Array ( [Deposit_ID] => 315 [Account_ID] => 2222100000010717 [Deposit_Type] => A [Check_Date] => 2019-08-09T00:00:00 [Check_Number] => 2783 [Deposit_Amount] => 210.00 [Deposit_Status] => NSF [NSF_Reason] => INSUF FUNDS [NSF_Date] => 2019-08-09T11:33:46.397 [NSF_Code] => R01 [Creation_Date] => 2019-08-09T11:32:00 [DRC_ClientID] => 2222 [DRC_TransactionID] => 9 ) ) ) 

    array (
  'Deposit_ID' => 315,
  'Account_ID' => '2222100000010717',
  'Deposit_Type' => 'A',
  'Check_Date' => '2019-08-09T00:00:00',
  'Check_Number' => 2783,
  'Deposit_Amount' => '210.00',
  'Deposit_Status' => 'NSF',
  'NSF_Reason' => 'INSUF FUNDS',
  'NSF_Date' => '2019-08-09T11:33:46.397',
  'NSF_Code' => 'R01                           ',
  'Creation_Date' => '2019-08-09T11:32:00',
  'DRC_ClientID' => '2222',
  'DRC_TransactionID' => 9,
)

Here is the multiple record result (note the indexing):这是多记录结果(注意索引):

Array ( [ReceiptsGetInfoResult] => Array ( [ReceiptObject] => Array ( [0] => Array ( [Deposit_ID] => 0 [Receipt_ID] => 2777 [Account_ID] => 4555100000010792 [ClientID] => 1127 [DateReceived] => 2019-01-07T16:22:46.927 [DateEntered] => 2019-01-07T16:23:00 [Deposit_Amount] => 1.0000 [Deposit_Type] => C [DRC_ClientID] => 1196 [HoldDate] => [CCDeposit_ID] => 35 [DRC_TransactionID] => 0 ) [1] => Array ( [Deposit_ID] => 0 [Receipt_ID] => 2779 [Account_ID] => 4555100000010792 [ClientID] => 1127 [DateReceived] => 2019-01-30T10:48:35.55 [DateEntered] => 2019-01-30T10:49:00 [Deposit_Amount] => 1.0000 [Deposit_Type] => C [DRC_ClientID] => 1196 [HoldDate] => [CCDeposit_ID] => 36 [DRC_TransactionID] => 0 ) [2] => Array ( [Deposit_ID] => 0 [Receipt_ID] => 2781 [Account_ID] => 2222100000010717 [ClientID] => 1141 [DateReceived] => 2019-08-08T00:00:00 [DateEntered] => 2019-08-08T14:09:00 [Deposit_Amount] => 100.0000 [Deposit_Type] => A [DRC_ClientID] => 2222 [HoldDate] => [CCDeposit_ID] => 0 [DRC_TransactionID] => ))))

    array (
  0 => 
  array (
    'Deposit_ID' => 0,
    'Receipt_ID' => 2777,
    'Account_ID' => '4555100000010792',
    'ClientID' => 1127,
    'DateReceived' => '2019-01-07T16:22:46.927',
    'DateEntered' => '2019-01-07T16:23:00',
    'Deposit_Amount' => '1.0000',
    'Deposit_Type' => 'C',
    'DRC_ClientID' => '1196',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 35,
    'DRC_TransactionID' => 0,
  ),
  1 => 
  array (
    'Deposit_ID' => 0,
    'Receipt_ID' => 2779,
    'Account_ID' => '4555100000010792',
    'ClientID' => 1127,
    'DateReceived' => '2019-01-30T10:48:35.55',
    'DateEntered' => '2019-01-30T10:49:00',
    'Deposit_Amount' => '1.0000',
    'Deposit_Type' => 'C',
    'DRC_ClientID' => '1196',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 36,
    'DRC_TransactionID' => 0,
  ),
  2 => 
  array (
    'Deposit_ID' => 0,
    'Receipt_ID' => 2781,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2019-08-08T00:00:00',
    'DateEntered' => '2019-08-08T14:09:00',
    'Deposit_Amount' => '100.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => NULL,
  ),
  3 => 
  array (
    'Deposit_ID' => 313,
    'Receipt_ID' => 2782,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2019-08-09T00:00:00',
    'DateEntered' => '2019-08-09T11:32:00',
    'Deposit_Amount' => '195.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => 8,
  ),
  4 => 
  array (
    'Deposit_ID' => 315,
    'Receipt_ID' => 2783,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2019-08-09T00:00:00',
    'DateEntered' => '2019-08-09T11:32:00',
    'Deposit_Amount' => '210.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => 9,
  ),
  5 => 
  array (
    'Deposit_ID' => 0,
    'Receipt_ID' => 2785,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2019-10-03T00:00:00',
    'DateEntered' => '2019-10-03T11:51:00',
    'Deposit_Amount' => '19.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => NULL,
  ),
  6 => 
  array (
    'Deposit_ID' => 0,
    'Receipt_ID' => 2787,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2019-09-28T00:00:00',
    'DateEntered' => '2019-10-03T11:52:00',
    'Deposit_Amount' => '28.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => '2019-09-28T00:00:00',
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => NULL,
  ),
  7 => 
  array (
    'Deposit_ID' => 0,
    'Receipt_ID' => 2788,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2019-09-29T00:00:00',
    'DateEntered' => '2019-10-03T11:52:00',
    'Deposit_Amount' => '29.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => '2019-09-29T00:00:00',
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => NULL,
  ),
  8 => 
  array (
    'Deposit_ID' => 0,
    'Receipt_ID' => 2789,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2019-09-30T00:00:00',
    'DateEntered' => '2019-10-03T11:53:00',
    'Deposit_Amount' => '30.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => NULL,
  ),
  9 => 
  array (
    'Deposit_ID' => 417,
    'Receipt_ID' => 2791,
    'Account_ID' => '2222100000010717',
    'ClientID' => 1141,
    'DateReceived' => '2020-01-16T00:00:00',
    'DateEntered' => '2020-01-16T11:15:00',
    'Deposit_Amount' => '130.0000',
    'Deposit_Type' => 'A',
    'DRC_ClientID' => '2222',
    'HoldDate' => NULL,
    'CCDeposit_ID' => 0,
    'DRC_TransactionID' => 7,
  ),
)

As I see it, I have to test for '[0]' and if zero exists then do X else do Y Count works with multiple records but does not work with the single record.正如我所见,我必须测试“[0]”,如果存在零,则执行 X 否则执行 Y Count 可处理多个记录但不适用于单个记录。 I tried array_key_exists but didn't have any luck I also tried array search but I'm not getting there...我尝试过 array_key_exists 但没有任何运气我也尝试过数组搜索但我没有到达那里......

My Question is: How do I test for index 0 in this mix?我的问题是:如何在此组合中测试索引 0? I'm open to any solutions that will work in this scenario.我对任何适用于这种情况的解决方案持开放态度。 Thanks much!非常感谢!

You have two different data sources here, so it's unclear why you would be running them through the same process, but you can easily tell which one is which.这里有两个不同的数据源,因此不清楚为什么要通过相同的过程运行它们,但您可以轻松分辨出哪个是哪个。

The command array_key_exists() works on the first level only , so based on the data provided, this should work without the hassle of determining if an array is indexed or associative:命令array_key_exists() 仅适用于第一级,因此根据提供的数据,这应该可以正常工作,而无需确定数组是索引还是关联:

<?php
if( array_key_exists( 'DepositsGetInfoResult', $dataUnderTest )) { 

    // this is the first type

} elseif ( array_key_exists( 'ReceiptsGetInfoResult', $dataUnderTest )) {

    // this is the second type

}

Now if you want to run them through the same foreach loop to pick out the common keys,现在,如果您想通过相同的 foreach 循环运行它们以挑选出公共键,

<?php
if( array_key_exists( 'DepositsGetInfoResult', $dataUnderTest )) { 

    // convert to an array similar to Receipts (add 0 index)
    $newArray = [ $dataUnderTest['DepositsGetInfoResult']['DepositObject'] ];

} elseif ( array_key_exists( 'ReceiptsGetInfoResult', $dataUnderTest )) {

    // extract part of Receipts
    $newArray = $dataUnderTest['ReceiptsGetInfoResult']['ReceiptObject'];

}

// the foreach loop you mentioned
foreach( $newArray as $record) {

    // do something

}

Thanks for your feedback.感谢您的反馈意见。 To clarify, these were two different tables but the same process is used for both.澄清一下,这是两个不同的表,但两者都使用相同的过程。 One had one record and the other had 10 records.一个有1个记录,另一个有10个记录。 I wasn't trying to blend them just test and foreach insert.我不是试图混合它们只是测试和 foreach 插入。 The problem occurs when one record is received.收到一条记录时会出现此问题。 That one record has no index associated with it.该记录没有与之关联的索引。 So array_key_exists wont work since it is recognized as an object.所以 array_key_exists 不会工作,因为它被识别为一个对象。 After working with this I came to the realization that we cant use array_key_exists.在处理完这个之后,我意识到我们不能使用 array_key_exists。 These two clues led me to the following conclusion that works:这两条线索使我得出了以下有效的结论:

$count1 = count($DepositsGetInfoArray); 

print_r($count1);打印_r($count1);

The above result is 1 - but its telling us that it recognizes it as an object which is confirmed by the below test (not that it is reading an index - there is no index).上面的结果是 1 - 但它告诉我们它把它识别为一个由下面的测试确认的对象(不是它正在读取一个索引 - 没有索引)。

if (!is_object($DepositsGetInfoArray)) { 
    echo '{"yes, its an object":"true"}';
} else {
    echo '{"no, its not an object":"false"}';

} }

The above returns "yes, its an object":"true" - confirming its a object以上返回“是的,它是一个对象”:“真” - 确认它是一个对象

so here's what I did:所以这就是我所做的:

I set up an if else to test for the object.我设置了一个 if else 来测试对象。 If it's an object make it an array with an index and if it's a count larger than one remove the headers and then process as data.如果它是一个对象,则使它成为一个带有索引的数组,如果它的计数大于 1,则删除标题,然后将其作为数据处理。

if ($count1 >1) { //anything larger than one when counting is counting indexes vs object

//REMOVES HEADERS AND MAKES DATA ARRAY ONLY (CONVERTS OBJECT INTO VARIABLE WITH DATA ONLY - NO HEADERS)
$arrDepositObjects = $DepositsGetInfoArray['DepositsGetInfoResult']['DepositObject'];

foreach($arrDepositObjects as $intKey => $data)

apply $sql应用 $sql

} else {

if ($count1 === 1) {

$array = Array($DepositsGetInfoArray); $array = Array($DepositsGetInfoArray); //APPLY AN INDEX TO THE ARRAY FOR LOOP PROCESSING //对数组应用索引进行循环处理

//DATA ARRAY -THIS REMOVES THE HEADERS -AND TURNS IT INTO data array ([0] => Array(and usable data) $arrDepositObjects = Array($DepositsGetInfoArray['DepositsGetInfoResult']['DepositObject']); //DATA ARRAY -THIS REMOVES THE HEADERS - AND TURNS IT INTO data array ([0] => Array(and usable data) $arrDepositObjects = Array($DepositsGetInfoArray['DepositsGetInfoResult']['DepositObject']);

foreach($arrDepositObjects as $intKey => $data)// run foreach foreach($arrDepositObjects as $intKey => $data)// 运行 foreach

apply $sql insert应用 $sql 插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM