views:

127

answers:

3

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?

+3  A: 

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!

Seb
A: 

yes, this is the only (reliable) way

stereofrog
+5  A: 

Yes.

function addItem($serializedArray, $item)
{
   $a = unserialize($serializedArray);
   $a[] = $item;
   return serialize($a);
}
Ewan Todd
If he's going to be using a function, it may just be more efficient to pass $serializedArray by reference and have it append the new item directly to the referenced array rather than returning the new value and setting it again.
BraedenP
@BraedenP. That sounds like it could be a good improvement. I think I would also take a bit more time to name the function and its variables.
Ewan Todd