无法用PHP反序列化数组的数组


Unable to un-serialize an array of arrays with PHP

数据库中的数组以序列化字符串的形式存储,例如:

a:1:{i:0;a:4:{s:8:"category";s:26:"Category Name";s:4:"date";s:0:"";s:8:"citation";s:617:"617 Char Length String (shortened on purpose)";s:4:"link";s:0:"";}}

当未序列化时,它的结构应该类似于以下内容:

array {
    id => array { category => Value, date => Value, citation => Value, link => Value }
}

我使用的php代码是:

$prevPubs = unserialize($result[0]['citations']);

$result[0]['citations']是序列化后的字符串。$prevPubs将返回false。如果我没弄错的话,这表示有错误。

b:0序列化为布尔值:false。Unserialize不会返回那个字符串,它只会返回一个布尔值FALSE。这意味着无论您传递给unserialize调用的是什么,都不是一个有效的序列化字符串。最有可能的是它被损坏了,导致反序列化调用失败。

为了处理序列化的多维数组和mysql使用这个:

<?php
//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));
//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));
?>

我很确定序列化字符串在你的数据库是损坏的

必须对整个字符串进行反序列化$result = unserialize($serialized);

,然后使用结果数组的$result[0]['citation']索引