How do I check if array value is empty?

前端 未结 8 1738
天命终不由人
天命终不由人 2020-12-09 04:03

Here is my array ouput

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

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

8条回答
  •  萌比男神i
    2020-12-09 04:34

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

提交回复
热议问题