PHP how can i append data into a serialized array

为君一笑 提交于 2019-12-10 14:57:19

问题


If I have a serialized array... how can I append more values to it? Should I unserialize it first -> add data and then serialize it again?


回答1:


Yes.

function addItem($serializedArray, $item)
{
   $a = unserialize($serializedArray);
   $a[] = $item;
   return serialize($a);
}



回答2:


Unserializing is the way to go, definitely. Unless you have a huge string, it'd be strongly recommended, unless you want to make your own strict interpreter.

Changing anything from a serialized array/object should be done very carefully - a single extra character would break everything if you don't update all previous numbers defining each piece of structure!




回答3:


yes, this is the only (reliable) way



来源:https://stackoverflow.com/questions/1658616/php-how-can-i-append-data-into-a-serialized-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!