簡體   English   中英

PHP和MySQL - 如何在函數中檢查數組是否為空

[英]PHP & MySQL - How to check if an array is empty or not in a function

我想知道如何檢查函數中的數組是否為空

這是我的代碼的一部分。

if (!mysqli_query($dbc, $sql)) {
        trigger_error(mysqli_error($dbc));
        return;
} else {
    $t = array();
    while($row = mysqli_fetch_array($result)) {
        $t[] = $row[0];
    }
}

if($tr > 0){
    $ts = array_sum($t);
}

使用empty()函數: http//php.net/manual/en/function.empty.php

松散的等價檢查在空數組上返回false

if(array()) // returns false

http://php.net/manual/en/types.comparisons.php

使用count()來計算數組中的元素數。

一種簡單的方法是使用count函數

例如:

if(count($sourceArray) != 0) {
    // Do exciting things here...
}
empty($t);

如果$t被認為是空的, $t true

要檢查數組是否為空:

if (sizeof($arr) == 0)
{
    //Empty array
}

要檢查數組中是否存在元素:

if (in_array($element, $arr))
{
     //Element found in the array
}

暫無
暫無

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

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