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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)