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
2009-11-01 22:37:18
+5
A:
Yes.
function addItem($serializedArray, $item)
{
$a = unserialize($serializedArray);
$a[] = $item;
return serialize($a);
}
Ewan Todd
2009-11-01 22:39:11
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
2009-11-01 22:57:26
@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
2009-11-01 23:01:20