Insertion into serialized array in PHP

This is my first time here, and I am pulling my hair out because of this, so I thought it would be a good first question for me.

I save the array data in a mysql database, and then later use unserialize to edit it. The problem is that it processes every other index in the array when I edit only one index. Here is a sample code:

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);
  }

Thanks for any insight you can give.

+3
source share
1 answer

for storing data in an array in mysql and for restoring and saving it in the same table, here is how to do it.

. , , .

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);

}

, . , , .

+2

Source: https://habr.com/ru/post/1762020/


All Articles