问题描述
我正在使用PHP/MySQL.
I am working in PHP/MySQL.
我的数据库中有一个名为hourly的表,该表中有一个名为webaddress的列,它们已被序列化.每个网址的每一行都有多行被序列化.
I have a table in my database called hourly in that table their is a column named webaddress these are serialized. There are multiple rows of each column of webaddresses each is serialized.
我需要拉出每一行,将它们解串,然后将它们放入数组中.
I need to pull each row, unserailize them then put them into an array.
我尝试使用这段代码,但是由于PHP函数的限制,它只能捕获1行.
I tried using this bit of code but it only grabs 1 row because of the limitations of the PHP functions.
while ($row = mysql_fetch_array($results)) {$test = unserialize($row[0]);}
我在想像这样的事情可能会起作用:
I was thinking something like this might work:
while(($row = mysql_fetch_array($results)) !== FALSE) {$test[] = $row;}
那行不通...
我该如何抓住每一行,然后将其解串,然后将其添加到数组中?我只需要web_addresses字段中的数据,当前有3行数据.因此,每个web_addresses字段中将有3个序列化的数组,我需要将它们反序列化并组合成另一个数组.希望这更有意义.
How can I grab each row, then unserailize it then add it to an array? I just need the data in the web_addresses field currently there is 3 rows of data. So there would be 3 serialized arrays in each web_addresses field that I need to unserialize and combine into another array. Hopefully that makes more sense.
这是MySQL表:
CREATE TABLE `hourly` (
`timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`id` bigint(20) NOT NULL auto_increment,
`month` longtext NOT NULL,
`day` longtext NOT NULL,
`year` longtext NOT NULL,
`source` longtext NOT NULL,
`web_address` longtext NOT NULL,
`at_replies` longtext NOT NULL,
`words` longtext NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1
推荐答案
编辑:现在根据OP的信息反映更新:
Now reflects updates based on info by the OP:
从您的问题开始,听起来每一行都有一个序列化的列,并且该列包含每行三项的序列化数组.所以这应该工作:
From your question it sounds like each row has one serialized column, and that column contains a serialized array of three items per rows. So this should work:
$collection = array();
while ( $row = mysql_fetch_array($results)) {
$values = unserialize($row[0]);
# $values has unserialized the data into its own array with 3 items
$collection = array_merge($collection, $values);
}
如果有3个数据库行,并且每个字段都有一个包含3个项目的序列化数组,则$ collection现在将包含一个包含9个项目的数组.
If there were three db rows, and each field had a serialized array with three items, $collection now contains an array with 9 items.
这篇关于mysql数据库中的序列化数据需要组合成一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!