In PHP, there are many ways to determine whether an array is empty. This article will introduce you to several methods in PHP to determine whether an array is empty.
<?php $arr = array(); // 空数组 if (empty($arr)) { echo "数组为空"; } else { echo "数组不为空"; } ?>
If $arr
is an empty array, the above code will output:
数组为空
If ## If there are elements in #$arr, the output will be:
数组不为空
<?php $arr = array(); // 空数组 if (count($arr) == 0) { echo "数组为空"; } else { echo "数组不为空"; } ?>
$arr is an empty array, the above The code will output:
数组为空
$arr, then it will output:
数组不为空
<?php $arr = array(); // 空数组 if (isset($arr) && count($arr) > 0) { echo "数组不为空"; } else { echo "数组为空"; } ?>
$arr is an empty array, the above code will output:
数组为空
$arr, then output:
数组不为空
<?php $arr = array(); // 空数组 if (array_key_exists(0, $arr)) { echo "数组不为空"; } else { echo "数组为空"; } ?>
$arr is an empty array, the above code will output:
数组为空
$arr element, the output is:
数组不为空
The above is the detailed content of [Organization and Sharing] Several ways to determine whether an array is empty in PHP. For more information, please follow other related articles on the PHP Chinese website!