duanpiyao2734 2017-05-31 17:49
浏览 237
已采纳

!数组键的empty()会显示“0”和NULL的值

I have an array with keys and values. The Keys are always set, the values might be "0" or NULL. What I want to do: Add all key-value-pairs which have a value into a new array. I use !empty() for this. Problem is: This loop also adds keys to the new array which contain NULL or "0".

Here is my code:

    // Loop over array and find all vars which are not empty
    $i = 0;
    foreach ($allInfoArray as $aKey=>$aVal) {
        if (!empty($aKey[$i])) {
            $relevantInfoArray[$aKey] = $aVal;
        }
        $i++;
    }

After that I uses var_dump() to check the new array.

array(11) { ["Key1"]=> string(3) "yes" ["Key2"]=> string(4) "1010" ["Key3"]=> string(4) "DED1" ["Key4"]=> string(7) "1234567" ["Group"]=> string(0) "" ["Dim"]=> string(0) "" ["Grd"]=> string(0) "" ["Nrm"]=> string(0) "" ["Flmc"]=> NULL ["Trmc"]=> NULL ["TrDim"]=> string(0) "" }

As you can see, the last values are all 0 or NULL. This also seems to happen randomly, other keys which have the value NULL or 0 are not added to this array.

Any ideas why these keys are added to the new array? Thanks a lot :)

  • 写回答

1条回答 默认 最新

  • douxuexiao1748 2017-05-31 17:55
    关注

    This doesn't answer why it isn't working, but it looks like you should be able to just use array_filter for this.

    $relevantInfoArray = array_filter($allInfoArray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行