简体   繁体   English

如何判断一个数组在子数组中是否有多个数组

[英]How to tell if an array has more than one array in the subarray

I have the following array with sub arrays [0] through [9].我有以下带有子数组 [0] 到 [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.因为有时我会从一些表中取回一条记录,我必须能够判断是否可以按原样保存它,还是应该使用 foreach 来保存它。 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.我正在构建一个 if-then 语句来确定是否有一个记录然后执行 x 或者如果有两个或更多然后执行 y 并使用 foreach 方法。 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注意:我使用的是 php 版本 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 2020 年 2 月 11 日起新增

this is the array key这是数组键

$array = Array($DepositsGetInfoArray);
print_r(array_keys($array));

Array ( [0] => 0 )数组 ( [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.注意:正如 Abra 所建议的那样,它可能会将其视为字符串。 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.再次感谢这里的任何帮助......谢谢。

The integer indexed subarray is under ['ReceiptsGetInfoResult']['ReceiptObject'] so just count that:整数索引子['ReceiptsGetInfoResult']['ReceiptObject']位于['ReceiptsGetInfoResult']['ReceiptObject']所以只需计算一下:

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

But actually a foreach is OK with one array element, it will only loop one time.但实际上foreach可以处理一个数组元素,它只会循环一次。 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
}

You can apply count() and do what you want:您可以应用count()并执行您想要的操作:

$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 
}

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详情: 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计数和更多有用的功能: 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详细信息: https : //www.php.net/manual/en/function.is-array.php

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

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