PHP 在 array_filter 中仅返回 0

我尝试仅在数组中的值仅为 0 时返回。因为 array_filter 自动过滤 0、null、empty 等,我需要使用自定义回调。< /span>


但是得到所有不是的回报null,empty


该数组是


[

    (int) 0 => '0',

    (int) 1 => '-100',

    (int) 2 => '100'

]


function countBETrades($arr) {

    $arrBE = array_filter($arr, function($v){

        return $v !== false && !is_null($v) && ($v != '' || $v == '0');

    });

    

    return count($arrBE);

}


慕标琳琳
浏览 69回答 1
1回答

ITMISS

如果您只想计算数字0,那么您只需要检查它(使用三元组=以强制 PHP检查类型,因此 null === 0 为 false):function countBETrades($arr) {&nbsp; &nbsp; $arrBE = array_filter($arr, function($v){&nbsp; &nbsp; &nbsp; &nbsp; return $v === 0 || $v === '0';&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return count($arrBE);}
打开App,查看更多内容
随时随地看视频慕课网APP