玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$gameresult数组到$gamerow?在这种情况下,如果$playerlist是空的,跳过爆炸可能会更有效,但为了讨论,我如何检查数组是否为空?

$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);

当前回答

这似乎适用于所有情况

if(!empty(sizeof($array)))

其他回答

你可以使用array_filter(),它适用于所有情况:

$ray_state = array_filter($myarray);

if (empty($ray_state)) {
    echo 'array is empty';
} else {
    echo 'array is not empty';
}

我认为确定数组是否为空的最好方法是像这样使用count():

if(count($array)) {
    return 'anything true goes here';
}else {
    return 'anything false'; 
}

在我看来,索引数组的最简单的方法是:

    if ($array) {
      //Array is not empty...  
    }

如果数组不为空,则数组上的'if'条件将计算为true,如果数组为空则为false。这不适用于关联数组。

 $gamerow = mysql_fetch_array($gameresult);

if (!empty(($gamerow['players'])) {
   $playerlist = explode(",", $gamerow['players']);
}else{
 
  // do stuff if array is empty
}
is_array($detect) && empty($detect);

is_array