簡體   English   中英

在PHP中插入序列化數組

[英]Inserting into a serialized array in PHP

這是我第一次來這里,我一直把頭發拉過來,所以我覺得這對我來說是個好問題。

我將一些數組數據保存到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);

}

以上只是為了給你一個想法。 我希望你已經明白應該如何在這里完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM