gpt4 book ai didi

php - 从 MySQL 到 PHP 的 boolean 值 - 转换为tinyint并进行比较

转载 作者:行者123 更新时间:2023-11-29 10:09:54 25 4
gpt4 key购买 nike

我的数据库有多个 boolean 值。保存数据库后, boolean 值被转换为tinyint(1)。我认为这是因为它只需要保存 1 或 0。

但是,我现在在比较 PHP 中的值时遇到问题。我将tinyint保存到一个数组中,没有任何代码转换。该数组有多个文本和日期条目以及多个 boolean 值条目,例如:

array[0] is '09:45:00'
array[1] is '10:45:00'
array[2] is 1
array[3] is 0
array[4] is 0
array[5] is 1
array[6] is 'active'

现在,如果我循环遍历数组,我想检查该值是否是时间、文本或真/假。

检查条目是否为 true 将始终返回 true,因为没有条目为空。检查条目是否为 1 或 0 适用于 boolean 值,但当我检查 'active' == 0 时,它返回 true。为什么会出现这种情况?如果我将字符串与tinyint 进行比较,如何得到 false?

与 === 进行比较在任何情况下都不起作用。

最佳答案

我认为你可以通过一些嵌套的 if-else 语句来做到这一点。但我很确定还有一个更好看的解决方案。 :)

$a=array('09:45:00','10:45:00',1,0,0,1,'active',3.12);

foreach ($a as $value) {
$type= gettype($value);
if ($type == "string") {
if(strtotime ($value)){
echo "$value is 'Time' \n";
}
else{
echo "$value is 'String' \n";
}
} elseif ($type == "integer") {
if($value == 0 || $value == 1){
echo "$value is 'Boolean' \n";
}
else{
echo "$value is 'Integer' \n";
}
} else{
echo "$value is ($type)!";
}
}

关于php - 从 MySQL 到 PHP 的 boolean 值 - 转换为tinyint并进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51186017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com