简体   繁体   中英

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.

Here is the array key (it is the same in every call): 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. I tried array_key_exists but didn't have any luck I also tried array search but I'm not getting there...

My Question is: How do I test for index 0 in this mix? 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:

<?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,

<?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. I wasn't trying to blend them just test and foreach insert. 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. After working with this I came to the realization that we cant use array_key_exists. These two clues led me to the following conclusion that works:

$count1 = count($DepositsGetInfoArray); 

print_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).

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 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.

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

} else {

if ($count1 === 1) {

$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']);

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

apply $sql insert

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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