douhan9619 2014-07-09 12:52
浏览 24
已采纳

php中的array_key_exists与NULL值没有输出

I have the following query:

//product density
$stmt_density = $conn->prepare("SELECT density FROM product_density
WHERE product_id = :productid ");
$stmt_density->execute(array(':productid' => "$pid"));
$density = $stmt_density->fetchAll(); 

If density is not present the value will be "NULL".

var_dump($density);

gives following output:

array(1) { [0]=> array(2) { ["density"]=> NULL [0]=> NULL } } 

I need to check whether the array is having values or not and to provide select options, if array not empty.

  • 写回答

3条回答 默认 最新

  • duankang5882 2014-07-09 13:43
    关注

    Depending on whether you expect to get back multiple results from your query could change the answer, especially if you get back say 1 result with a NULL and one without. But you could do...

    function validResult($density) {
        foreach($density as $item) {
            if($item["density"] == NULL) return false;
        }
        return true;
     }
    
    
     if(validResult($density))
         echo "We have values";
     else
         echo "Array contains null";
    

    The function will return false if any result has null, regardless of how many are not null

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?