玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$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 ($arr) == 0:计数 Empty ($arr):空 $arr == []: comp (bool) $arr:类型转换
得到了以下结果
Contents \method | count | empty | comp | cast |
------------------|--------------|--------------|--------------|--------------|
Empty |/* 1.213138 */|/* 1.070011 */|/* 1.628529 */| 1.051795 |
Uniform |/* 1.206680 */| 1.047339 |/* 1.498836 */|/* 1.052737 */|
Integer |/* 1.209668 */|/* 1.079858 */|/* 1.486134 */| 1.051138 |
String |/* 1.242137 */| 1.049148 |/* 1.630259 */|/* 1.056610 */|
Mixed |/* 1.229072 */|/* 1.068569 */|/* 1.473339 */| 1.064111 |
Associative |/* 1.206311 */| 1.053642 |/* 1.480637 */|/* 1.137740 */|
------------------|--------------|--------------|--------------|--------------|
Total |/* 7.307005 */| 6.368568 |/* 9.197733 */|/* 6.414131 */|
empty和强制转换为布尔类型的区别是没有意义的。我已经多次运行这个测试,它们看起来基本上是相同的。数组的内容似乎没有发挥重要作用。这两者产生了相反的结果,但逻辑上的否定几乎不足以推动施法在大多数情况下获胜,所以我个人更喜欢空,因为在任何一种情况下都是易读的。
#!/usr/bin/php
<?php
// 012345678
$nt = 90000000;
$arr0 = [];
$arr1 = [];
$arr2 = [];
$arr3 = [];
$arr4 = [];
$arr5 = [];
for ($i = 0; $i < 500000; $i++) {
$arr1[] = 0;
$arr2[] = $i;
$arr3[] = md5($i);
$arr4[] = $i % 2 ? $i : md5($i);
$arr5[md5($i)] = $i;
}
$t00 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr0) == 0;
}
$t01 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr0);
}
$t02 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr0 == [];
}
$t03 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr0;
}
$t04 = microtime(true);
$t10 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr1) == 0;
}
$t11 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr1);
}
$t12 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr1 == [];
}
$t13 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr1;
}
$t14 = microtime(true);
/* ------------------------------ */
$t20 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr2) == 0;
}
$t21 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr2);
}
$t22 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr2 == [];
}
$t23 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr2;
}
$t24 = microtime(true);
/* ------------------------------ */
$t30 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr3) == 0;
}
$t31 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr3);
}
$t32 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr3 == [];
}
$t33 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr3;
}
$t34 = microtime(true);
/* ------------------------------ */
$t40 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr4) == 0;
}
$t41 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr4);
}
$t42 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr4 == [];
}
$t43 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr4;
}
$t44 = microtime(true);
/* ----------------------------------- */
$t50 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
count($arr5) == 0;
}
$t51 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
empty($arr5);
}
$t52 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
$arr5 == [];
}
$t53 = microtime(true);
for ($i = 0; $i < $nt; $i++) {
(bool) $arr5;
}
$t54 = microtime(true);
/* ----------------------------------- */
$t60 = $t00 + $t10 + $t20 + $t30 + $t40 + $t50;
$t61 = $t01 + $t11 + $t21 + $t31 + $t41 + $t51;
$t62 = $t02 + $t12 + $t22 + $t32 + $t42 + $t52;
$t63 = $t03 + $t13 + $t23 + $t33 + $t43 + $t53;
$t64 = $t04 + $t14 + $t24 + $t34 + $t44 + $t54;
/* ----------------------------------- */
$ts0[1] = number_format(round($t01 - $t00, 6), 6);
$ts0[2] = number_format(round($t02 - $t01, 6), 6);
$ts0[3] = number_format(round($t03 - $t02, 6), 6);
$ts0[4] = number_format(round($t04 - $t03, 6), 6);
$min_idx = array_keys($ts0, min($ts0))[0];
foreach ($ts0 as $idx => $val) {
if ($idx == $min_idx) {
$ts0[$idx] = " $val ";
} else {
$ts0[$idx] = "/* $val */";
}
}
$ts1[1] = number_format(round($t11 - $t10, 6), 6);
$ts1[2] = number_format(round($t12 - $t11, 6), 6);
$ts1[3] = number_format(round($t13 - $t12, 6), 6);
$ts1[4] = number_format(round($t14 - $t13, 6), 6);
$min_idx = array_keys($ts1, min($ts1))[0];
foreach ($ts1 as $idx => $val) {
if ($idx == $min_idx) {
$ts1[$idx] = " $val ";
} else {
$ts1[$idx] = "/* $val */";
}
}
$ts2[1] = number_format(round($t21 - $t20, 6), 6);
$ts2[2] = number_format(round($t22 - $t21, 6), 6);
$ts2[3] = number_format(round($t23 - $t22, 6), 6);
$ts2[4] = number_format(round($t24 - $t23, 6), 6);
$min_idx = array_keys($ts2, min($ts2))[0];
foreach ($ts2 as $idx => $val) {
if ($idx == $min_idx) {
$ts2[$idx] = " $val ";
} else {
$ts2[$idx] = "/* $val */";
}
}
$ts3[1] = number_format(round($t31 - $t30, 6), 6);
$ts3[2] = number_format(round($t32 - $t31, 6), 6);
$ts3[3] = number_format(round($t33 - $t32, 6), 6);
$ts3[4] = number_format(round($t34 - $t33, 6), 6);
$min_idx = array_keys($ts3, min($ts3))[0];
foreach ($ts3 as $idx => $val) {
if ($idx == $min_idx) {
$ts3[$idx] = " $val ";
} else {
$ts3[$idx] = "/* $val */";
}
}
$ts4[1] = number_format(round($t41 - $t40, 6), 6);
$ts4[2] = number_format(round($t42 - $t41, 6), 6);
$ts4[3] = number_format(round($t43 - $t42, 6), 6);
$ts4[4] = number_format(round($t44 - $t43, 6), 6);
$min_idx = array_keys($ts4, min($ts4))[0];
foreach ($ts4 as $idx => $val) {
if ($idx == $min_idx) {
$ts4[$idx] = " $val ";
} else {
$ts4[$idx] = "/* $val */";
}
}
$ts5[1] = number_format(round($t51 - $t50, 6), 6);
$ts5[2] = number_format(round($t52 - $t51, 6), 6);
$ts5[3] = number_format(round($t53 - $t52, 6), 6);
$ts5[4] = number_format(round($t54 - $t53, 6), 6);
$min_idx = array_keys($ts5, min($ts5))[0];
foreach ($ts5 as $idx => $val) {
if ($idx == $min_idx) {
$ts5[$idx] = " $val ";
} else {
$ts5[$idx] = "/* $val */";
}
}
$ts6[1] = number_format(round($t61 - $t60, 6), 6);
$ts6[2] = number_format(round($t62 - $t61, 6), 6);
$ts6[3] = number_format(round($t63 - $t62, 6), 6);
$ts6[4] = number_format(round($t64 - $t63, 6), 6);
$min_idx = array_keys($ts6, min($ts6))[0];
foreach ($ts6 as $idx => $val) {
if ($idx == $min_idx) {
$ts6[$idx] = " $val ";
} else {
$ts6[$idx] = "/* $val */";
}
}
echo " | count | empty | comp | cast |\n";
echo "-------------|--------------|--------------|--------------|--------------|\n";
echo " Empty |";
echo $ts0[1] . '|';
echo $ts0[2] . '|';
echo $ts0[3] . '|';
echo $ts0[4] . "|\n";
echo " Uniform |";
echo $ts1[1] . '|';
echo $ts1[2] . '|';
echo $ts1[3] . '|';
echo $ts1[4] . "|\n";
echo " Integer |";
echo $ts2[1] . '|';
echo $ts2[2] . '|';
echo $ts2[3] . '|';
echo $ts2[4] . "|\n";
echo " String |";
echo $ts3[1] . '|';
echo $ts3[2] . '|';
echo $ts3[3] . '|';
echo $ts3[4] . "|\n";
echo " Mixed |";
echo $ts4[1] . '|';
echo $ts4[2] . '|';
echo $ts4[3] . '|';
echo $ts4[4] . "|\n";
echo " Associative |";
echo $ts5[1] . '|';
echo $ts5[2] . '|';
echo $ts5[3] . '|';
echo $ts5[4] . "|\n";
echo "-------------|--------------|--------------|--------------|--------------|\n";
echo " Total |";
echo $ts6[1] . '|';
echo $ts6[2] . '|';
echo $ts6[3] . '|';
echo $ts6[4] . "|\n";
其他回答
在PHP中,空数组是错误的,因此甚至不需要像其他人建议的那样使用empty()。
<?php
$playerList = array();
if (!$playerList) {
echo "No players";
} else {
echo "Explode stuff...";
}
// Output is: No players
PHP的empty()确定变量是否存在或值是否为假值(如array(), 0, null, false等)。
在大多数情况下,你只想检查!$emptyVar。使用empty($emptyVar)如果变量可能没有设置,你不愿意触发一个E_NOTICE;在我看来,这是个坏主意。
在我看来,索引数组的最简单的方法是:
if ($array) {
//Array is not empty...
}
如果数组不为空,则数组上的'if'条件将计算为true,如果数组为空则为false。这不适用于关联数组。
一些不错的答案,但我只是想展开一点,以更清楚地解释PHP何时确定数组是否为空。
主要记录:
带有一个键(或多个键)的数组将被PHP判定为非空。
由于数组值需要键才能存在,数组中是否有值并不决定数组是否为空,只有在没有键(因此也没有值)的情况下才会决定数组是否为空。
因此,用empty()检查数组并不只是告诉你是否有值,而是告诉你数组是否为空,键是数组的一部分。
因此,在决定使用哪种检查方法之前,请考虑如何生成数组。 当用户提交HTML表单时,当每个表单字段都有一个数组名称(即name="array[]"),数组就会有键。 将为每个字段生成一个非空数组,因为每个表单字段的数组将有自动递增的键值。
以这些数组为例:
/* Assigning some arrays */
// Array with user defined key and value
$ArrayOne = array("UserKeyA" => "UserValueA", "UserKeyB" => "UserValueB");
// Array with auto increment key and user defined value
// as a form field would return with user input
$ArrayTwo[] = "UserValue01";
$ArrayTwo[] = "UserValue02";
// Array with auto incremented key and no value
// as a form field would return without user input
$ArrayThree[] = '';
$ArrayThree[] = '';
如果你回显上述数组的数组键和值,你会得到以下结果:
一号阵: [UserKeyA] => [UserValueA] [UserKeyB] => [UserValueB] 二阵: [0] => [UserValue01] [1] => [UserValue02] 三阵: [0] => [] [1] => []
并且用empty()测试上述数组会返回以下结果:
数组: $ArrayOne不是空的 两个数组: $ArrayTwo不是空的 组三: $ArrayThree不是空的
当你赋值一个数组但之后不使用它时,数组将始终为空,例如:
$ArrayFour = array();
这将是空的,即PHP在上面使用if empty()时将返回TRUE。
因此,如果你的数组有键-例如通过表单的输入名称或如果你手动分配它们(即创建一个数据库列名作为键的数组,但没有数据库中的值/数据),那么数组将不是空的()。
在这种情况下,您可以在foreach中循环数组,测试每个键是否有一个值。如果您无论如何都需要遍历数组,比如检查键或清除数据,这是一个好方法。
然而,如果你只是需要知道“值是否存在”返回TRUE或FALSE,这不是最好的方法。 当一个数组知道它将有键时,有各种方法来确定它是否有值。函数或类可能是最好的方法,但它总是取决于您的环境和确切的需求,以及其他事情,例如您当前如何处理数组(如果有的话)。
下面是一种方法,它使用很少的代码来检查数组是否有值:
使用array_filter (): 遍历数组中的每个值,将它们传递给回调函数。如果回调函数返回true,则数组中的当前值返回到结果数组中。数组键被保留。
$EmptyTestArray = array_filter($ArrayOne);
if (!empty($EmptyTestArray))
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
在所有三个示例数组上运行array_filter()(在这个答案的第一个代码块中创建),结果如下:
数组: $arrayone不是空的 两个数组: $arraytwo不是空的 组三: $arraythree为空
因此,当没有值时,无论是否有键,使用array_filter()创建一个新数组,然后检查新数组是否为空,显示原始数组中是否有任何值。 这并不理想,而且有点混乱,但如果您有一个巨大的数组,并且由于任何其他原因不需要循环它,那么就所需的代码而言,这是最简单的。
我在检查开销方面没有经验,但如果能知道使用array_filter()和foreach检查是否找到值之间的区别,那就更好了。
显然,基准测试需要在各种参数上,在大小数组上,当有值时,等等。
Array_filter递归和计数
function array_filter_recursive(array $arr)
{
array_walk($arr,function(&$item){if (is_array($item)) { $item = array_filter_recursive($item);}});
return array_filter($arr);
}
function is_empty_array(array $arr):bool{
return count(array_filter_recursive($arr)) == 0;
}
test
$c=['b'=>2,'c'=>3];
$a=[];
$b=[[]];
$d=['a'=>[]];
$e=['a'=>[],[]];
$f=['a'=>[[],[],[]],[]];
$g=[[[],[[],[[],[[],[]]]]],[]];
$i=[[[],[[],[[],[[],['s'=>1]]]]],[]];
var_dump(is_empty_array($c));//false
var_dump(is_empty_array($a));//true
var_dump(is_empty_array($b));//true
var_dump(is_empty_array($d));//true
var_dump(is_empty_array($e));//true
var_dump(is_empty_array($f));//true
var_dump(is_empty_array($g));//true
var_dump(is_empty_array($i));//false
为什么没有人回答:
$array = [];
if($array == []) {
// array is empty
}