Last Updated: February 25, 2016
·
4.552K
· danjesus

Safe serialize & unserialize PHP

//to safely serialize
$safe_string_to_store = base64_encode(serialize($multidimensional_array));

//to unserialize...
$array_restored_from_db = unserialize(base64_decode($encoded_serialized_string));

Errata

Use jsondecode and jsonencode instead of serialize and unserialize, use these functions as well as lighter, simplifies the use of these data in the frontend.

I had not thought of that thanks! @zaus @mbarzda the comment

json_encode($array);
json_decode($string);

3 Responses
Add your response

@mbarzda Mentioned insurance in order not to corrupt data, agree with json_encode for large data. Thanks!

over 1 year ago ·

doesn't base64 also result in ~20% larger content? json_*code ftw, since you can use it directly in the frontend.

over 1 year ago ·

@zaus @mbarzda thanks! I had not thought of it that way, I will insert an errata in the tip

over 1 year ago ·