【问题标题】:Cannot Unserialize an object array in php无法在php中反序列化对象数组
【发布时间】:2009-12-17 03:42:35
【问题描述】:

我在 PHP 中有一个 Shift 对象数组。我需要将这些对象存储在数据库中。我正在开发一个向数据库添加班次的函数:

$Serialized_S = get_option('MasterShiftString');
$MasterShiftArray = unserialize($Serialized_S);

if(!$MasterShiftArray)
{
    echo "MasterShiftArray returns false";
}//end if

echo "Serialized_S:";
print_r($Serialized_S); 
echo "<br />MasterShiftString:";
print_r($MasterShiftString); 
echo "<br />end<br />"; 



if(!is_array($MasterShiftArray))
{
    echo "MasterShiftArray is not an Array....";
    $MasterShiftArray = array($last_monday_from_date => "");

}//end if
else
{


}//end else 

$WeekShiftArray = $MasterShiftArray;

array_push($WeekShiftArray, $CurrentShift);         

$MasterShiftArray[$last_monday_from_date] = $WeekShiftArray;

$Serialized_s = serialize($MasterShiftArray);

update_option('MasterShiftArray', $Serialized_s);

当然,当我执行这个时我得到的是:

last_monday_from_date: 1260777600
MasterShiftArray returns falseSerialized_S:admin,resource,2,1;admin,resource,2,1;admin,resource,2,1;admin,resource,2,1;
MasterShiftString:
end

我在这里做错了什么?我已经尝试过 base64 编码,但这无济于事。 MasterShiftArray 不是数组......

【问题讨论】:

    标签: php serialization


    【解决方案1】:

    这个:

    admin,resource,2,1;admin,resource,2,1;admin,resource,2,1;admin,resource,2,1
    

    看起来一点也不像 PHP 序列化数组,那是你的问题。垃圾进 = 垃圾出。

    假设这是您需要处理的数据格式,请考虑使用 explode 将其分解为 ';' 上的数组,然后在 ',' 上分解该数组的每个成员。

    【讨论】:

    • 天啊……对不起……我现在感觉自己像个白痴……我输入了错误的变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 2016-12-30
    • 1970-01-01
    • 2012-04-15
    • 2019-04-12
    • 2016-02-01
    相关资源
    最近更新 更多