簡體   English   中英

如何判斷一個數組在子數組中是否有多個數組

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

我有以下帶有子數組 [0] 到 [9] 的數組。 我想測試數組,看看是否有一個子數組或多個。 為什么? 因為有時我會從一些表中取回一條記錄,我必須能夠判斷是否可以按原樣保存它,還是應該使用 foreach 來保存它。 我正在構建一個 if-then 語句來確定是否有一個記錄然后執行 x 或者如果有兩個或更多然后執行 y 並使用 foreach 方法。 我還沒有能夠隔離子數組並正確計算它們來做出這個決定。 任何幫助,將不勝感激。 所需要的只是如何測試一個或多個。 謝謝。 注意:我使用的是 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 ) ) ) )

2020 年 2 月 11 日起新增

這是數組鍵

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

數組 ( [0] => 0 )

注意:單個記錄沒有與之關聯的索引,並且來自任何表的任何單個記錄都會出現這種情況 -

 $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 所建議的那樣,它可能會將其視為字符串。 所以我仍然無法區分一個記錄和多個記錄。 但挑戰仍然存在——如何做到這一點。 這里有一些提示:

這將返回“假”

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";
}

不幸的是沒有做任何事情。 再次感謝這里的任何幫助......謝謝。

整數索引子['ReceiptsGetInfoResult']['ReceiptObject']位於['ReceiptsGetInfoResult']['ReceiptObject']所以只需計算一下:

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

但實際上foreach可以處理一個數組元素,它只會循環一次。 有什么問題?

如果它可能根本不是數組,請檢查它:

if(is_array($array['ReceiptsGetInfoResult']['ReceiptObject'])) {
    //foreach
} else {
    //don't foreach
}

您可以應用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 
}

所有解決方案都會起作用

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

詳情: https : //www.php.net/manual/en/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

或者

這將檢查數組中的元素是否為數組

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

詳細信息: https : //www.php.net/manual/en/function.is-array.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM