How to check whether an array is empty using PHP?(如何使用 PHP 检查数组是否为空?)
问题描述
players
将为空或以逗号分隔的列表(或单个值).检查它是否为空的最简单方法是什么?我假设我可以在将 $gameresult
数组获取到 $gamerow
后立即这样做?在这种情况下,如果 $playerlist
为空,则跳过爆炸可能会更有效,但为了论证,我将如何检查数组是否也为空?
players
will either be empty or a comma separated 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']);
推荐答案
如果只需要检查数组中是否有ANY元素
If you just need to check if there are ANY elements in the array
if (empty($playerlist)) {
// list is empty.
}
如果您需要在检查前清除空值(通常这样做是为了防止 explode
处理奇怪的字符串):
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 检查数组是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 PHP 检查数组是否为空?


基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01