PHP 判断数组是否为空的四种方法
可以使用以下方法来判断一个数组是否为空:
- 使用 empty 函数
if(empty($arr)){
// 数组为空
}
- 使用 count 函数
if(count($arr) == 0){
// 数组为空
}
- 使用 array_filter 函数
if(empty(array_filter($arr))){
// 数组为空
}
- 使用 foreach 循环
$isEmpty = true;
foreach($arr as $value){
$isEmpty = false;
break;
}
if($isEmpty){
// 数组为空
}
原文地址: https://www.cveoy.top/t/topic/oYjt 著作权归作者所有。请勿转载和采集!