如何检查数组值是否为空?

时间:2021-01-09 07:18:44

Here is my array ouput

这是我的阵列输出

Array
(
    [1] => 1
    [2] => 2
    [3] =>  
)

How do I know the [3] => is empty?

我怎么知道[3] =>是空的?

foreach ($array as $key => $value) {
    if (empty($value))
        echo "$key empty <br/>";
    else
        echo "$key not empty <br/>";
}

My out put showing all is not empty. What is correct way to check is empty?

我的出局显示全部并非空洞。什么是正确的检查方法是空的?

7 个解决方案

#1


13  

It works as expected, third one is empty

它按预期工作,第三个是空的

http://codepad.org/yBIVBHj0

Maybe try to trim its value, just in case that third value would be just a space.

也许试着修剪它的值,以防第三个值只是一个空格。

foreach ($array as $key => $value) {
    $value = trim($value);
    if (empty($value))
        echo "$key empty <br/>";
    else
        echo "$key not empty <br/>";
}

#2


19  

An other solution:

另一个解决方案:

$array = array('one', 'two', '');

if(count(array_filter($array)) == count($array)) {
    echo 'OK';
} else {
    echo 'ERROR';
}

http://codepad.org/zF9KkqKl

#3


4  

You can use array_diff() and array_diff_key():

您可以使用array_diff()和array_diff_key():

$array = array('one', 'two', '');
$emptyKeys = array_diff_key(array_diff($array,array()),$array);

array_diff() extracts all items which are not the same (therefore leaving out the blanks), array_diff_key gives back the differences to the original array.

array_diff()提取所有不相同的项(因此省略空白),array_diff_key返回与原始数组的差异。

#4


3  

You can check for an empty array by using the following:

您可以使用以下命令检查空数组:

if ( !empty(array_filter($array))) {
    echo 'OK';
} else {
    echo 'EMPTY ARRAY';
}

#5


0  

Here is a simple solution to check an array for empty key values and return the key.

这是一个简单的解决方案,用于检查数组中的空键值并返回键。

$a = array('string', '', 5);

        echo array_search(null, $a);
        // Echos 1

To check if array contains an empty key value. Try this.

检查数组是否包含空键值。尝试这个。

        $b = array('string','string','string','string','','string');

        if (in_array(null, $b)) {
            echo 'We found a empty key value in your array!';
        }

#6


0  

im using in my project like this for check this array

我在我的项目中使用这样来检查这个数组

im posting form data like this array('username' => 'john','surname' => 'sins');

即发布表格数据,如此数组('username'=>'john','surname'=>'sins');

public function checkArrayKeyExist($arr) {
    foreach ($arr as $key => $value) {
        if (!strlen($arr[$key])) {
            return false;
        }
    }
    return true;
}

#7


0  

Try this:

<?php
    $data=array(
        'title' => 'Test Name Four',
        'first_name' => '',
        'last_name' => 'M',
        'field_company' => 'ABC',
        'email' => '',
        'client_phone_number' => '',
        'address_line_1' => '',
        'address_line_2' => 'Address 3',
        'address_line_3' => '',
        'address_line_4' => '',
        'post_code' => '',
        );
    echo '<pre>';
    print_r($data);
    foreach ($data as $key => $case ) { 
        echo "$key => ".is_multiArrayEmpty($case)."<br>"; 
    }
    function is_multiArrayEmpty($multiarray) { 
        if(is_array($multiarray) and !empty($multiarray)){ 
            $tmp = array_shift($multiarray); 
                if(!is_multiArrayEmpty($multiarray) or !is_multiArrayEmpty($tmp)){ 
                    return false; 
                } 
                return true; 
        } 
        if(empty($multiarray)){ 
            return true; 
        } 
        return false; 
    } 
?>

#1


13  

It works as expected, third one is empty

它按预期工作,第三个是空的

http://codepad.org/yBIVBHj0

Maybe try to trim its value, just in case that third value would be just a space.

也许试着修剪它的值,以防第三个值只是一个空格。

foreach ($array as $key => $value) {
    $value = trim($value);
    if (empty($value))
        echo "$key empty <br/>";
    else
        echo "$key not empty <br/>";
}

#2


19  

An other solution:

另一个解决方案:

$array = array('one', 'two', '');

if(count(array_filter($array)) == count($array)) {
    echo 'OK';
} else {
    echo 'ERROR';
}

http://codepad.org/zF9KkqKl

#3


4  

You can use array_diff() and array_diff_key():

您可以使用array_diff()和array_diff_key():

$array = array('one', 'two', '');
$emptyKeys = array_diff_key(array_diff($array,array()),$array);

array_diff() extracts all items which are not the same (therefore leaving out the blanks), array_diff_key gives back the differences to the original array.

array_diff()提取所有不相同的项(因此省略空白),array_diff_key返回与原始数组的差异。

#4


3  

You can check for an empty array by using the following:

您可以使用以下命令检查空数组:

if ( !empty(array_filter($array))) {
    echo 'OK';
} else {
    echo 'EMPTY ARRAY';
}

#5


0  

Here is a simple solution to check an array for empty key values and return the key.

这是一个简单的解决方案,用于检查数组中的空键值并返回键。

$a = array('string', '', 5);

        echo array_search(null, $a);
        // Echos 1

To check if array contains an empty key value. Try this.

检查数组是否包含空键值。尝试这个。

        $b = array('string','string','string','string','','string');

        if (in_array(null, $b)) {
            echo 'We found a empty key value in your array!';
        }

#6


0  

im using in my project like this for check this array

我在我的项目中使用这样来检查这个数组

im posting form data like this array('username' => 'john','surname' => 'sins');

即发布表格数据,如此数组('username'=>'john','surname'=>'sins');

public function checkArrayKeyExist($arr) {
    foreach ($arr as $key => $value) {
        if (!strlen($arr[$key])) {
            return false;
        }
    }
    return true;
}

#7


0  

Try this:

<?php
    $data=array(
        'title' => 'Test Name Four',
        'first_name' => '',
        'last_name' => 'M',
        'field_company' => 'ABC',
        'email' => '',
        'client_phone_number' => '',
        'address_line_1' => '',
        'address_line_2' => 'Address 3',
        'address_line_3' => '',
        'address_line_4' => '',
        'post_code' => '',
        );
    echo '<pre>';
    print_r($data);
    foreach ($data as $key => $case ) { 
        echo "$key => ".is_multiArrayEmpty($case)."<br>"; 
    }
    function is_multiArrayEmpty($multiarray) { 
        if(is_array($multiarray) and !empty($multiarray)){ 
            $tmp = array_shift($multiarray); 
                if(!is_multiArrayEmpty($multiarray) or !is_multiArrayEmpty($tmp)){ 
                    return false; 
                } 
                return true; 
        } 
        if(empty($multiarray)){ 
            return true; 
        } 
        return false; 
    } 
?>