序列化php中的布尔整数显示

时间:2015-10-01 03:24:55

标签: php serialization

为什么保存序列化数据后它会给我一个布尔输出,我输入的结果为b:1?以下是我数据库中保存的数据。

a:2:{s:30:"features_safety_number_airbags";b:1;s:29:"features_safety_trans_control";b:1;}

这是插入代码。

if(isset($_POST['submit'])){

        $add_item_features = array(
            "features_safety_number_airbags" => isset($_POST['safety_airbags']),
            "features_safety_trans_control" => isset($_POST['safety_traction']),
            );

        $add_item_features_serialize = serialize($add_item_features);

        $sql = "INSERT INTO item_newcar_temp(`item_features`) VALUES ('".$add_item_features_serialize."')";

1 个答案:

答案 0 :(得分:0)

这是因为您实际上是将数组值设置为isset()函数的返回值,这是一个布尔值。

var_dump(isset($a)); // will return boolean(true/false)

你想要的是Ternary Operator,看起来像这样:

$add_item_features = array(
    "features_safety_number_airbags" => ((isset($_POST['safety_airbags'])) ? $_POST['safety_airbags'] : 'it wasnt set..'),
    "features_safety_trans_control" => ((isset($_POST['safety_traction'])) ? $_POST['safety_traction'] : 'it wasnt set..'),
);

将返回类似于:

的序列化项目
a:2:{s:30:"features_safety_number_airbags";s:14:"it wasnt set..";s:29:"features_safety_trans_control";s:14:"it wasnt set..";}

Demo/Example