How to check array value is empty in php

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?

How to check array value is empty in php

JConstantine

3,8691 gold badge31 silver badges45 bronze badges

asked Feb 20, 2012 at 19:47

Unknown ErrorUnknown Error

7676 gold badges14 silver badges25 bronze badges

2

An other solution:

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

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

http://codepad.org/zF9KkqKl

answered Nov 18, 2014 at 10:00

How to check array value is empty in php

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

answered Feb 20, 2012 at 19:49

Martin.Martin.

10.3k3 gold badges40 silver badges67 bronze badges

0

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

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

How to check array value is empty in php

Stephen Rauch

45.6k30 gold badges105 silver badges126 bronze badges

answered Dec 9, 2017 at 4:16

0

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.

answered Feb 20, 2012 at 19:53

How to check array value is empty in php

konsolenfreddykonsolenfreddy

9,4821 gold badge24 silver badges36 bronze badges

1

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

answered Feb 13, 2019 at 6:54

How to check array value is empty in php

jay thankijay thanki

1,0841 gold badge11 silver badges17 bronze badges

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

answered Sep 25, 2015 at 7:10

How to check array value is empty in php

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

answered Sep 5, 2016 at 9:29

2

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

answered Dec 15, 2014 at 10:33

How to check array value is empty in php

Sharma VikramSharma Vikram

2,3525 gold badges22 silver badges44 bronze badges

How do you check if a value in an array is empty?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

Is empty array falsey PHP?

For example, in PHP, empty arrays are falsy, but in JavaScript arrays are always truthy.

How do you check if an array has a value in PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

Is null or empty PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .