unserialize() 期望参数 1 为字符串,数组给定
unserialize() expects parameter 1 to be string, array given
我正在通过序列化方法将数据存储在mysql table中,现在我想打印所有数据所以我写了mysql查询并尝试反序列化,因为数据是序列化格式但反序列化显示错误。
错误:
unserialize() expects parameter 1 to be string, array given
查询以获取所有记录
$this->db->select('*');
$this->db->from($table);
$result=$this->db->get()->result();
$unserialize_list=unserialize($result);
您的 $result
变量包含一个 multi-dimensional 数组。
假设 table 中的某些数据已序列化,并且由于您尚未发布 table 架构,这里有一个示例 table 我希望它符合您的使用案例:
Id | Data
-------------------
1 | a:2:{s:4:"Name";s:5:"Test1";s:8:"Location";s:9:"Somewhere";}
2 | a:2:{s:4:"Name";s:5:"Test2";s:8:"Location";s:14:"Somewhere Else";}
3 | a:2:{s:4:"Name";s:5:"Test3";s:8:"Location";s:18:"Somewhere Else Too";}
运行 此代码:
$this->db->select('*');
$this->db->from($table);
$result=$this->db->get()->result();
$unserialize_list=unserialize($result);
将生成一个对象数组,table 中的每一行对应一个对象,如下所示:
Array
(
[0] => stdClass Object
(
[Id] => 1
[Data] => a:2:{s:4:"Name";s:5:"Test1";s:8:"Location";s:9:"Somewhere";}
)
[1] => stdClass Object
(
[Id] => 2
[Data] => a:2:{s:4:"Name";s:5:"Test2";s:8:"Location";s:14:"Somewhere Else";}
)
[2] => stdClass Object
(
[Id] => 2
[Data] => a:2:{s:4:"Name";s:5:"Test3";s:8:"Location";s:18:"Somewhere Else Too";}
)
)
您需要运行以下代码才能访问未序列化的数据:
foreach ($result as $line) {
$unserializedData = unserialize($line->Data);
// Use the unserialized data as needed...
print_r($unserializedData);
}
我正在通过序列化方法将数据存储在mysql table中,现在我想打印所有数据所以我写了mysql查询并尝试反序列化,因为数据是序列化格式但反序列化显示错误。
错误:
unserialize() expects parameter 1 to be string, array given
查询以获取所有记录
$this->db->select('*');
$this->db->from($table);
$result=$this->db->get()->result();
$unserialize_list=unserialize($result);
您的 $result
变量包含一个 multi-dimensional 数组。
假设 table 中的某些数据已序列化,并且由于您尚未发布 table 架构,这里有一个示例 table 我希望它符合您的使用案例:
Id | Data
-------------------
1 | a:2:{s:4:"Name";s:5:"Test1";s:8:"Location";s:9:"Somewhere";}
2 | a:2:{s:4:"Name";s:5:"Test2";s:8:"Location";s:14:"Somewhere Else";}
3 | a:2:{s:4:"Name";s:5:"Test3";s:8:"Location";s:18:"Somewhere Else Too";}
运行 此代码:
$this->db->select('*');
$this->db->from($table);
$result=$this->db->get()->result();
$unserialize_list=unserialize($result);
将生成一个对象数组,table 中的每一行对应一个对象,如下所示:
Array
(
[0] => stdClass Object
(
[Id] => 1
[Data] => a:2:{s:4:"Name";s:5:"Test1";s:8:"Location";s:9:"Somewhere";}
)
[1] => stdClass Object
(
[Id] => 2
[Data] => a:2:{s:4:"Name";s:5:"Test2";s:8:"Location";s:14:"Somewhere Else";}
)
[2] => stdClass Object
(
[Id] => 2
[Data] => a:2:{s:4:"Name";s:5:"Test3";s:8:"Location";s:18:"Somewhere Else Too";}
)
)
您需要运行以下代码才能访问未序列化的数据:
foreach ($result as $line) {
$unserializedData = unserialize($line->Data);
// Use the unserialized data as needed...
print_r($unserializedData);
}