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

问题描述:

Here is my array ouput

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

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

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?

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/>";
}

You can use array_diff() and 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.

An other solution:

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

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

http://codepad.org/zF9KkqKl

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; 
    } 
?>

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!';
        }

im using in my project like this for check this array

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

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

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

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

$array = array('A', 'B', ''); 

or

$array = array('A', 'B', '  ');

An other solution:

this work for me

if(in_array(null, $myArray) || in_array('', array_map('trim',$myArray))) {
   echo 'Found a empty value in your array!';
   }