How do I check if array value is empty?

前端 未结 8 1725
天命终不由人
天命终不由人 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条回答
  •  独厮守ぢ
    2020-12-09 04:25

    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 
    "; else echo "$key not empty
    "; }

提交回复
热议问题