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条)

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误