如何判断一个PHP数组是空的? [英] How to tell if a PHP array is empty?
问题描述
玩家
要么是空的,或者一个逗号分隔的列表(或单个值)。什么是检查它是否是空的最简单的方法?我假设我可以这样,只要我取 $ gameresult
数组 $ gamerow
吗?在这种情况下,它可能会更有效地跳过爆炸的 $ playerlist
如果它是空的,但对于论证起见,我将如何检查是否一个数组是空的,以及?
players
will either be empty or a comma seperated list (or a single value). What is the easiest way to check if it's empty? I'm assuming I can do so as soon as I fetch the $gameresult
array into $gamerow
? In this case it would probably be more efficient to skip exploding the $playerlist
if it's empty, but for the sake of argument, how would I check if an array is empty as well?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
谢谢!
推荐答案
如果你只需要检查是否有数组中的任何元素
If you just need to check if there are ANY elements in the array
if (empty($playerlist)) {
// list is empty.
}
如果你需要检查之前清理出空值(一般做prevent 爆炸
ING怪异的字符串):
If you need to clean out empty values before checking (generally done to prevent explode
ing weird strings):
foreach ($playerlist as $key => $value) {
if (empty($value)) {
unset($playerlist[$key]);
}
}
if (empty($playerlist)) {
//empty array
}
这篇关于如何判断一个PHP数组是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!