I have the following array with sub arrays [0] through [9]. I want to test the array and see if there is One sub array or more than one. Why? Because sometimes I get one record back from some tables and I have to be able to tell whether I can save it as is or should I use a foreach to save it. I’m building an if-then statement to determine if one record then do x or if there is two or more then do y and use the foreach method. I have yet to be able to isolate the sub array and count them correctly to make this determination. Any help would be appreciated. All that is needed is how to test for one or more than one. Thanks. Note: I’m on php version 7.2.7
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] => ) [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] => [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] => [CCDeposit_ID] => 0 [DRC_TransactionID] => 9 ) [5] => Array ( [Deposit_ID] => 0 [Receipt_ID] => 2785 [Account_ID] => `enter code here`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] => [CCDeposit_ID] => 0 [DRC_TransactionID] => ) [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] => ) [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] => ) [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] => [CCDeposit_ID] => 0 [DRC_TransactionID] => ) [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] => [CCDeposit_ID] => 0 [DRC_TransactionID] => 7 ) ) ) )
New as of 2/11/2020
this is the array key
$array = Array($DepositsGetInfoArray);
print_r(array_keys($array));
Array ( [0] => 0 )
Note: the single record does not have an index associated with it and will be the case with any single records coming over from any table –
$arrDepositObjects = $DepositsGetInfoArray['DepositsGetInfoResult']['DepositObject'];
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 ) ) )
Note: as Abra suggested it may be treating it as a string. So I am still not able to discern between one record and multiple records. But the challenge remains – how to do this. Here are some hints:
This returns “false”
echo 'TESTING IF COUNT :';
if (count($arrDepositObjects['DepositsGetInfoResult']['DepositObject']) === 1){
echo '{"yes, its equal to one":"true"}';
} else {
echo '{"undetermined":"false"}';
}
AND THIS
echo "<hr>";
function is_assoc($arrDepositObjects)
{
return is_array($arrDepositObjects) && array_diff_key($arrDepositObjects,array_keys(array_keys($arrDepositObjects)));
}
function test($var)
{
echo is_assoc($arrDepositObjects) ? "I'm an assoc array.n" : "I'm not an assoc array.n";
}
does not do anything unfortunately. Again, any help here is appreciated…Thanks.
3
Answers
The integer indexed subarray is under
['ReceiptsGetInfoResult']['ReceiptObject']
so just count that:But actually a
foreach
is OK with one array element, it will only loop one time. What’s the problem?If it may not be an array at all then check it:
You can apply
count()
and do what you want:All solutions will work
Details : https://www.php.net/manual/en/function.sizeof.php
Or
Count and more usefull functions : https://www.php.net/manual/en/function.array-count-values.php
Or
This will check if element in array is an array or not
details on : https://www.php.net/manual/en/function.is-array.php