Home >Backend Development >PHP Problem >How to determine if an array is empty in php

How to determine if an array is empty in php

PHPz
PHPzOriginal
2023-04-20 13:54:33966browse

In PHP, sometimes you need to determine whether an array is empty. Normally, we use the empty() or count() function to determine whether an array is empty, but if the values ​​of all elements in the array are NULL or 0, these functions may get wrong results. Therefore, this article will introduce how to accurately determine whether an array is empty.

1. Limitations of the empty() function

The empty() function is a commonly used function in PHP to determine whether a variable is empty. It can determine whether a variable is empty. If the variable is undefined or its value is false, '', 0, '0', null, array(), etc., it returns true, otherwise it returns false. Therefore, when judging whether an array is empty, we usually write like this:

$arr = array();
if(empty($arr)) {
    echo '数组为空';
} else {
    echo '数组不为空';
}

This method seems very simple, but when the values ​​​​of all elements in the array are NULL or 0, empty( ) function will return true, which is obviously not the result we want. For example:

$arr = array(null, 0, 0.0, false, '');
if(empty($arr)) {
    echo '数组为空';
} else {
    echo '数组不为空';
}

The above code will return the array as empty, which is incorrect.

2. Problems with the count() function

Another commonly used method to determine whether an array is empty is to use the count() function, which can return the number of elements in an array. If there are no elements in an array, the count() function returns 0, otherwise it returns the number of elements in the array. Therefore, the code to determine whether an array is empty usually looks like this:

$arr = array();
if(count($arr) == 0) {
    echo '数组为空';
} else {
    echo '数组不为空';
}

However, when the values ​​​​of all elements in an array are NULL or 0, the count() function will also return 0, which means Can lead to misjudgment. For example:

$arr = array(null, 0, 0.0, false, '');
if(count($arr) == 0) {
    echo '数组为空';
} else {
    echo '数组不为空';
}

The above code will return an empty array, which is not the result we want.

3. Accurately determine whether an array is empty

In order to solve the above problem, we need to accurately determine whether an array is empty. An accurate way to determine is to use a foreach loop to traverse the array and check whether the value of each element in the array is empty. If a non-empty element is checked, the array is not empty, otherwise the array is empty. The following is a code example:

function is_empty_array($arr) {
    foreach($arr as $value) {
        if(!empty($value)) {
            return false;
        }
    }
    return true;
}

$arr = array(null, 0, 0.0, false, '');
if(is_empty_array($arr)) {
    echo '数组为空';
} else {
    echo '数组不为空';
}

The above code will output "array is empty", which is the correct result we need.

The above is the PHP method introduced in this article to determine if an array is not empty. Using a foreach loop to traverse the array can avoid the limitations of the empty() and count() functions and accurately determine whether an array is empty.

The above is the detailed content of How to determine if an array is empty in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn