PHP“ NA” vs 0 vs null

时间:2019-12-21 20:45:31

标签: php

几天前,我使用的API返回了array

中的某些数据
$data = [
    "amount" => 0, //integer
    "last_login" => "NA", //string
    "active" => false //boolean
    ...
];

我想将所有“ NA”都强制转换为boolean(以false的形式使用)

foreach ($data as $key => $value) {

    if($value == "NA"){ //if value is equal to "NA" set as false
        $data[$key] = false;
    }

}

预期输出为:

Array
(
    [amount] => 0
    [last_login] => false
    [active] => false
)

但是我得到了:

Array
(
    [amount] => false //here breaks if amount is passed as 0, because expected Integer for some calculations
    [last_login] => false
    [active] => false
)

经过几小时的破坏,我发现php0视为NA(“ NA” == 0):

var_dump("NA" == 0); //bool(true)
var_dump(0 == null); //bool(true)
var_dump("NA" == null); //bool(false)

//more
var_dump(empty("NA")); //bool(false)
var_dump(empty(null)); //bool(true)
var_dump(empty(0)); //bool(true)

这里是一个问题,为什么NA等于0,我试图在php.net上找到DataTypes的引用表,但是没有提及NA及其相关内容与0相等。

0 个答案:

没有答案