如何反序列化,使用序列化存储在数据库中的数据?
请举个例子。
$result = array();
foreach ($upload_data as $file) {
$result[] = $file['file'];
}
$insert_to_database = serialize($result);
//This inserted in database, I want unserialize this output
output: a:2:{i:0;s:55:"Chrysanthemum18.jpg";i:1;s:48:"Desert18.jpg";}
答案 0 :(得分:3)
$var = array('a', 'b', 'c');
$var = serialize($var);
var_dump($var); // string(42) "a:3:{i:0;s:1:"a";i:1;s:1:"b";i:2;s:1:"c";}"
$var = unserialize($var);
echo $var[0]; // 'a'
答案 1 :(得分:2)
假设您使用CI AR来检索序列化数据......
$qry = $this->db->select('your_serialized_field')
->from('some_table')
->get();
$serialized_data = $qry->result_array();
$unserialized_data = array();
// Then for every record...
foreach($serialized_data as $single_record)
{
$unserialized_data[] = unserialize($single_record);
}
// You now can see your array
var_dump($unserialized_data);