gpt4 book ai didi

php - Drupal PHP 序列化和反序列化

转载 作者:搜寻专家 更新时间:2023-10-31 21:36:09 26 4
gpt4 key购买 nike

在 Drupal 中,我首先序列化出现在私有(private)消息正文中的电子邮件,并将它们存储在 MySQL 中,如下所示:

function prvtmsg_list($body) {
$notify = array();
if (isset($body->emails)) {
$notify['mid'] = $body->mid;
$notify['emails'] = serialize($body->emails);
}
if (isset($body->vulgar_words) {
$notify['mid'] = $body->mid;
$notify['vulgar_words'] = serialize($message->vulgar_words);
}
if (isset($notify['mid'])) {
drupal_write_record('prvtmsg_notify', $notify);
}
}

当我稍后尝试检索它们时,电子邮件用户化失败,我这样检索它们:

function prvtmsg_list_notify() {
// Select fields from prvtmsg_notify and Drupal pm_message tables
$query = db_select('prvtmsg_notify', 'n');
$query->leftJoin('pm_message', 'm', 'n.mid = m.mid');
$query->fields('n', array('mid', 'emails', 'vulgar_words'));
$query->fields('m', array('mid', 'author', 'subject', 'body', 'timestamp'));
orderBy('timestamp', 'DESC');
$query = $query->extend('PagerDefault')->limit(20);
$result = $query->execute()->fetchAll();

$rows = array();
foreach ($result as $notify) {
$rows[] = array(
$notify->author,
$notify->subject,
implode(', ', unserialize($notify->emails)),
implode(', ', unserialize($notify->vulgar_words)),
);
}

$build = array();
$build['table'] = array(
'#theme' => 'table',
'#header' => array(
t('Author'),
t('Message subject'),
t('Emails captured'),
t('Vulgar Words Captured'),
),
'#rows' => $rows,
);
$build['pager']['#theme'] = 'pager';

return $build;

也许我序列化邮件的方式不对?因为:

dpm(unserialize($notify->emails);

给出 Array, Array, Array - 这意味着:

数组(
[0] => 数组() [1] => 数组() [2] => 数组() [3] => 数组()
)

令人惊讶的是,未序列化的粗俗文字显示正常!我不确定是否可以像这样序列化电子邮件:

$notify['emails'] = serialize (array($body->emails));

我过去遇到过反序列化对我不起作用的确切情况,我有一些不清楚的地方,我需要学习它。谁能确认或告诉我哪里出了问题?

注意以上代码来自内存,可能不准确,因为我目前无法访问它。

最佳答案

如果我没看错,你就是在将数组写入数据库

drupal_write_record('prvtmsg_notify', $notify);

应该是:

drupal_write_record('prvtmsg_notify', serialize($notify));

你很可能不再需要

$notify['emails'] = serialize($body->emails);

也可以这样写:

$notify['emails'] = $body->emails;

从数据库中检索后,您可以反序列化数组并对其进行迭代,例如:

$array = unserialize(someFunctionToGetPrvtmsg_notifyFromTheDb());
//the array should be the same as the one you serialized

关于php - Drupal PHP 序列化和反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19071869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com