玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$gameresult数组到$gamerow?在这种情况下,如果$playerlist是空的,跳过爆炸可能会更有效,但为了讨论,我如何检查数组是否为空?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$gameresult数组到$gamerow?在这种情况下,如果$playerlist是空的,跳过爆炸可能会更有效,但为了讨论,我如何检查数组是否为空?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
当前回答
您可以使用以下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。
数组为空。
其他回答
我使用这个代码
$variable = array();
if( count( $variable ) == 0 )
{
echo "Array is Empty";
}
else
{
echo "Array is not Empty";
}
但请注意,如果数组有大量的键,与这里的其他答案相比,这段代码将花费大量时间来计算它们。
Count ($gamerow['players'])将为0。
做出最恰当的决策需要了解数据的质量以及要遵循的流程。
如果你要取消/忽略/删除这一行,那么最早的过滤点应该在mysql查询中。
哪里玩家不是空的 哪里的玩家!= " WHERE COALESCE(玩家,")!= " 哪里的球员不是NULL和球员!= " ...这取决于你的存储数据,还有其他方法,我就讲到这里。
If you aren't 100% sure if the column will exist in the result set, then you should check that the column is declared. This will mean calling array_key_exists(), isset(), or empty() on the column. I am not going to bother delineating the differences here (there are other SO pages for that breakdown, here's a start: 1, 2, 3). That said, if you aren't in total control of the result set, then maybe you have over-indulged application "flexibility" and should rethink if the trouble of potentially accessing non-existent column data is worth it. Effectively, I am saying that you should never need to check if a column is declared -- ergo you should never need empty() for this task. If anyone is arguing that empty() is more appropriate, then they are pushing their own personal opinion about expressiveness of scripting. If you find the condition in #5 below to be ambiguous, add an inline comment to your code -- but I wouldn't. The bottom line is that there is no programmatical advantage to making the function call. Might your string value contain a 0 that you want to deem true/valid/non-empty? If so, then you only need to check if the column value has length.
下面是一个使用strlen()的演示。这将指示字符串是否会在分解时创建有意义的数组元素。
I think it is important to mention that by unconditionally exploding, you are GUARANTEED to generate a non-empty array. Here's proof: Demo In other words, checking if the array is empty is completely useless -- it will be non-empty every time. If your string will NOT POSSIBLY contain a zero value (because, say, this is a csv consisting of ids which start from 1 and only increment), then if ($gamerow['players']) { is all you need -- end of story. ...but wait, what are you doing after determining the emptiness of this value? If you have something down-script that is expecting $playerlist, but you are conditionally declaring that variable, then you risk using the previous row's value or again generating Notices. So do you need to unconditionally declare $playerlist as something? If there are no truthy values in the string, does your application benefit from declaring an empty array? Chances are, the answer is yes. In this case, you can ensure that the variable is array-type by falling back to an empty array -- this way it won't matter if you feed that variable into a loop. The following conditional declarations are all equivalent.
If ($gamerow['players']) {$playerlist =爆炸(',',$gamerow['players']);} else {$playerlist = [];} $playerlist = $gamerow['玩家']?explosion (',', $gamerow['players']): [];
为什么我要花这么长时间来解释这个非常基本的任务?
I have whistleblown nearly every answer on this page and this answer is likely to draw revenge votes (this happens often to whistleblowers who defend this site -- if an answer has downvotes and no comments, always be skeptical). I think it is important that Stackoverflow is a trusted resource that doesn't poison researchers with misinformation and suboptimal techniques. This is how I show how much I care about upcoming developers so that they learn the how and the why instead of just spoon-feeding a generation of copy-paste programmers. I frequently use old pages to close new duplicate pages -- this is the responsibility of veteran volunteers who know how to quickly find duplicates. I cannot bring myself to use an old page with bad/false/suboptimal/misleading information as a reference because then I am actively doing a disservice to a new researcher.
这似乎适用于所有情况
if(!empty(sizeof($array)))
我认为确定数组是否为空的最好方法是像这样使用count():
if(count($array)) {
return 'anything true goes here';
}else {
return 'anything false';
}