问题描述
我有以下具有子数组[0]至[9]的数组.我想测试该数组,看看是否有一个子数组或多个子数组.为什么?因为有时我会从某些表中获得一条记录,并且我必须能够告诉我是否可以按原样保存它,还是应该使用foreach来保存它.我正在建立一个if-then语句,以确定一条记录是否然后执行x或是否存在两个或多个然后执行y并使用foreach方法.我尚未能够隔离子数组并正确计数它们来做出此确定.任何帮助,将不胜感激.所有需要做的就是如何测试一个或多个.谢谢.注意:我使用的是php版本7.2.7
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 ) ) ) )
截至2020年2月11日的新内容
New as of 2/11/2020
这是阵列键
$array = Array($DepositsGetInfoArray);
print_r(array_keys($array));
数组([0] => 0)
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 ) ) )
注意:正如Abra建议的那样,可能会将其视为字符串.因此,我仍然无法区分一个记录和多个记录.但是挑战仍然存在-如何做到这一点.这里有一些提示:
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:
这将返回"false"
This returns "false"
echo 'TESTING IF COUNT :';
if (count($arrDepositObjects['DepositsGetInfoResult']['DepositObject']) === 1){
echo '{"yes, its equal to one":"true"}';
} else {
echo '{"undetermined":"false"}';
}
这
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.
推荐答案
所有解决方案都可以使用
All solutions will work
if (sizeof($array) > 1) {
//do what ever you want
}else{
//do what ever you want
}
详细信息: https://www.php.net/manual/zh-CN/function.sizeof.php
或
if (count($array) > 1) {
//do what ever you want
}else{
//do what ever you want
}
计数和更有用的功能: https://www .php.net/manual/en/function.array-count-values.php
Count and more usefull functions : https://www.php.net/manual/en/function.array-count-values.php
或
这将检查数组中的元素是否为数组
if(is_array($array['ReceiptsGetInfoResult']['ReceiptObject'])) {
// This element is an array
}
详细信息: https://www.php.net/manual/zh-CN/function.is-array.php
这篇关于如何判断一个数组在子数组中是否有多个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!