这是我第一次来这里,我一直在为这个问题紧张,所以我认为这对我来说是一个很好的第一个问题。
我正在将一些数组数据保存到 mysql 数据库中,然后我使用 unserialize 对其进行编辑。问题是当我只编辑一个索引时,它会清空数组中的所有其他索引。下面是一些示例代码:
foreach($post as $key => $value) {
if (isset($row)) {
if ($i > 2) { $tempArray = unserialize($row[$i]); }
}
$tempArray[$time] = $value;
if ($key == 'pid' || $key == 'uid') { $data[$key] = $value; }
else { $data[$key] = serialize($tempArray); }
$i += 1;
unset($tempArray);
}
感谢您提供的任何见解。
最佳答案
要在 mysql 中存储序列化数组数据并将其检索并保存回同一个表,这是您必须执行的操作。
从表中获取序列化数据并将其反序列化并将数组数据存储在变量中。通过使用我们存储数据的变量来更改数组数据。
我们再次序列化具有修改数组的变量,并使用新的序列化数据更新 mysql 表字段。
例如:
$data = array(
"uid" => "some value",
"pid" => "some value"
);
$store_in_database = serialize($data);
/* ...do the mysql insert query to store the new data in the field (serializedfield)... */
现在更新存储在数据库表字段中的序列化数据。
/* ... 以任何你喜欢的方式获取表的行或记录(单条记录或多条记录)*/
foreach($db_result as $db_row){
$temp_array = $db_row['serializedfield'];
$temp_array['uid'] = "a new value";
$temp_array['pid'] = "another new value";
/* ... i wanted to add another array value */
$temp_array['akey'] = "some value";
$store_database = serialize($temp_array);
/* ... do the mysql update query to replace this new data with the old one. */
unset($temp_array);
}
以上只是给你一个想法。我希望您已经了解应该如何在此处完成。
关于php - 在 PHP 中插入序列化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3588957/