PHP& MySQL - 如何在函数中检查数组是否为空

时间:2010-12-22 19:04:04

标签: php mysql

我想知道如何在函数

中检查数组是否为空

这是我的代码的一部分。

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

6 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

松散的等价检查在空数组上返回false

if(array()) // returns false

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

答案 2 :(得分:0)

使用count()计算数组中元素的数量。

答案 3 :(得分:0)

一种简单的方法是使用count function

例如:

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

答案 4 :(得分:0)

empty($t);
如果true被视为空,则

会向您$t

答案 5 :(得分:0)

检查数组是否为空:

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

检查数组中是否存在元素:

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