Home    Articles


Unserialize offset notice/error after serializeing array in php


Notice: unserialize(): Error at offset 2103 of 512312 bytes in myFile.php

This error may be confusing if you used serialize() function before trying to unserialize. This error apears becouse the string that must be unserialized is corrupted. 

This corruption can happen for multiple reasons, but two are the most common:

1. Array values contains one of these characters: : , " , ' , ...these caracters are used to separate arrays from string in order to be reconized when the unserialize happends. Serialize was created to save multidimensional array with integer values, not text. If you want to serialize array with values as text use json_encode and json_decode to reverse.

2. You have saved the serialized string into your database:  - you need to be carefoul becouse if you use column type as text, you have to be sure that your array is not too large. If your string goes over 65.000 bites your string will be cuted. Use longtext as column type in this case.

!!! Also make sure you use base64_encode and base64_decode...this will increase your chances to have a valid string to be unserialized.

Here is how:

// serialize
$serializedString = base64_encode(serialize($myArray));

// unserialize...
$unserializedString = unserialize(base64_decode($serializedString))