玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$gameresult数组到$gamerow?在这种情况下,如果$playerlist是空的,跳过爆炸可能会更有效,但为了讨论,我如何检查数组是否为空?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
玩家要么是空的,要么是逗号分隔的列表(或者是单个值)。检查它是否为空的最简单的方法是什么?我假设我可以这样做,只要我取回$gameresult数组到$gamerow?在这种情况下,如果$playerlist是空的,跳过爆炸可能会更有效,但为了讨论,我如何检查数组是否为空?
$gamerow = mysql_fetch_array($gameresult);
$playerlist = explode(",", $gamerow['players']);
当前回答
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
}
我运行了文章末尾包含的基准测试。比较方法:
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;在我看来,这是个坏主意。
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
如果你只需要检查数组中是否有任何元素,你可以使用数组本身,因为PHP的松散类型,或者-如果你喜欢更严格的方法-使用count():
if (!$playerlist) {
// list is empty.
}
if (count($playerlist) === 0) {
// list is empty.
}
如果你需要在检查之前清理空值(通常是为了防止奇怪的字符串爆炸):
foreach ($playerlist as $key => $value) {
if (!strlen($value)) {
unset($playerlist[$key]);
}
}
if (!$playerlist) {
//empty array
}