-1

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.

Dak
  • 21
  • 5
  • Possibly answered [here](https://stackoverflow.com/questions/262891/is-there-a-way-to-find-out-how-deep-a-php-array-is)...? At least to point you in the right direction of how to check the depth of an array. – Kam Jan 30 '20 at 21:07
  • @KamaranL: That is overkill based on what they asked and their example. – AbraCadaver Jan 30 '20 at 21:11
  • 1
    @Dak Please always present your object/array data as `echo "
    "; var_export($yourArray);` when posting on Stackoverflow.   There is so much horizontal scrolling to do with the current layout.  Also, how does the data vary? Is the target subarray sometimes not an array? If it is always an array, just use a loop as @Abra has explained.
    – mickmackusa Jan 30 '20 at 22:17

3 Answers3

0

The integer indexed subarray is under ['ReceiptsGetInfoResult']['ReceiptObject'] so just count that:

$count = count($array['ReceiptsGetInfoResult']['ReceiptObject']);

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:

if(is_array($array['ReceiptsGetInfoResult']['ReceiptObject'])) {
    //foreach
} else {
    //don't foreach
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • `is_array` will be always true -> useless `else`. OP said **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** – Aksen P Jan 30 '20 at 21:22
  • I found an anomaly and I don't know why it is occurring. When one record exists it scatters the data. It wants to draw one character from each field and save that one. Yet it works fine with the loop on multiple records. INSERT INTO tblNSF (Deposit_ID, Account_ID, Deposit_Type, Check_Date, Check_Number, Deposit_Amount, Deposit_Status, NSF_Reason, NSF_Date, NSF_Code, Creation_Date, DRC_ClientID, DRC_TransactionID) VALUES ('', '', '', '', '', '', '', '', '', '', '', '','') 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 A A A A A A A A A A A A A – Dak Jan 31 '20 at 14:21
  • Because when there is one record it is not an array it is a string and however you are looping loops the characters in the string probably. You need to show the difference between the two types of arrays in your question. – AbraCadaver Jan 31 '20 at 14:25
0

You can apply count() and do what you want:

$array = ["ReceiptsGetInfoResult" => ["ReceiptObject" => [["ClientID"=>122],["ClientID"=>123],["ClientID"=>124],]]];

if (count($array["ReceiptsGetInfoResult"]["ReceiptObject"]) === 1){
    // code for case X
} else if (count($array["ReceiptsGetInfoResult"]["ReceiptObject"]) > 1){
    // code for case Y 
}
Aksen P
  • 4,564
  • 3
  • 14
  • 27
  • Why `elseif` and not just `> 1` and `else`? – AbraCadaver Jan 30 '20 at 21:18
  • @AbraCadaver, OP said definitely 1 and more than 1. BTW your `in_array` will be always an array. – Aksen P Jan 30 '20 at 21:19
  • You are counting a element in array, not counting array –  Jan 30 '20 at 21:44
  • [does this answer your questions](https://3v4l.org/ee1d4), "programmers"? – Aksen P Jan 30 '20 at 21:55
  • This yielded false when testing a single record: echo 'TESTING IF COUNT :'; if (count($arrDepositObjects['DepositsGetInfoResult']['DepositObject']) === 1){ echo '{"yes, its equal to one":"true"}'; } else { echo '{"undetermined":"false"}'; } – Dak Jan 31 '20 at 15:48
  • Present your array with one subarray which you'd tested. Also, your question sounds next **if an array has more than one array in subarray**. It means your subarray always contains arrays. Otherwise, you'd really unclear question and everyone can only guess how to help you. – Aksen P Jan 31 '20 at 16:31
0

All solutions will work

if (sizeof($array) > 1) {
    //do what ever you want
}else{
    //do what ever you want
}

Details : https://www.php.net/manual/en/function.sizeof.php

Or

if (count($array) > 1) {
    //do what ever you want
}else{
    //do what ever you want
}

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

if(is_array($array['ReceiptsGetInfoResult']['ReceiptObject'])) {
   // This element is an array
}

details on : https://www.php.net/manual/en/function.is-array.php

  • I ran this on similar object, and got a false response: if(is_array($arrDepositObjects['DepositsGetInfoResult']['DepositObject'])) { // This element is an array echo '{"yes, its an array":"true"}'; } else { echo '{"no, its not an array":"false"}'; } – Dak Jan 31 '20 at 15:15
  • I then ran the if (count($array) > 1) { ... and it displayed true. How can two different answers be received here from an object perspective? What's going on in the background? – Dak Jan 31 '20 at 15:22
  • See given link for example I am on mobile, you can return array with var_dumb() or print_r() or just echo out this is an array if it’s true. That code checks if an element in array is an another array. See given link in answer for each solution. –  Jan 31 '20 at 15:30
  • Check this link instead of given url for Count : https://www.php.net/manual/en/function.count.php given url in answer was for alternative functions to use for the same result of count –  Jan 31 '20 at 15:39
  • See this quick example for is array I created on mobile : https://3v4l.org/iLYCf –  Jan 31 '20 at 16:03