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);
Written by Dan Jesus
Related protips
3 Responses
@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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Related Tags
#php
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#