开发者

Empty Array won't be "empty"

I got an array, filled with things out of a MySQL. Sometimes there isn't any data, so I tried checking with empty() to see if I should drop the emtpy array or if it contains any data. I tried it with an empty array but empty() gives m开发者_如何学JAVAe a "full" array, even if print_r shows an empty.

Array ( [0] => Array ( [0] => Array ( ) ) )
Array ( [0] => Array ( [0] => Array ( ) ) )


empty() will return true for array(), FALSE, NULL, '', 0, and '0'. An empty array would be empty(), but an array containing an empty array would not.


Assuming $array is the array in your example, try this:

$array = array_filter( $array );
if ( empty( $array ) )
{
    // do what you want
}


To do what I think you're trying to do, you need to write a recursive function that will traverse through every item in every level of your multidimensional array and check the empty() value of every element. I think this is what you're after.

function isEmpty($array){
    $empty = true;
    foreach($array as $value){
        if(is_array($value)){
            if(!isEmpty($value)) $empty = false;
        }else{
            if(!empty($value)) $empty = false;
        }
    }
    return $empty;
}

Then just run isEmpty() on your array.


I believe BraedenP is correct.

However, a far superior solution is to make changes in the place where you create that array. In that context, it should be really easy to detect whether the number of rows in the SQL resultset is 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜