玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$gameresult数组到$gamerow?在这种情况下,如果$playerlist是空的,跳过爆炸可能会更有效,但为了讨论,我如何检查数组是否为空?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$gameresult数组到$gamerow?在这种情况下,如果$playerlist是空的,跳过爆炸可能会更有效,但为了讨论,我如何检查数组是否为空?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
当前回答
Count ($gamerow['players'])将为0。
其他回答
Count ($gamerow['players'])将为0。
您可以使用以下php函数来检查数组是否为空
使用empty()函数
$variable = array();
if(empty($variable)){
echo("The array is empty.");
}
OUTPUT:数组为空
使用sizeof()函数
$variable = array();
$arraysize = sizeof($variable);
echo("The size of the array is $arraysize. \n");
if(sizeof($variable) == 0)
echo("The array is empty.");
输出:
数组的大小为0。
数组为空。
empty($gamerow['players'])
$gamerow = mysql_fetch_array($gameresult);
if (!empty(($gamerow['players'])) {
$playerlist = explode(",", $gamerow['players']);
}else{
// do stuff if array is empty
}
$status = "";
$new_array = array();
if(!empty($new_array)){
$status = "1"; // not a blank array
}
else{
$status = "0"; // blank array
}