Home >Backend Development >PHP Tutorial >Applying PHP Serialization Array Techniques_PHP Tutorial
serialize --------- Format the array into an ordered string
unserialize ----- Restore the array into Array
PHP serialized array test example:
$test = array("a"=>0,"b"=> ;0,"c"=>0);
$test2 = '';
$test2=serialize($test);
echo $test2;
echo "
PHP serialization array usage:
In my personal experience, it mainly deals with array transfer and array storage operate.
For example, I have an array that needs to be passed to the next page. If you don’t want to use seesion/cookie, then you can use this function, pass it, and then restore it.
For example, when I was making a website directory, there was a rating, which was divided into positive, medium and negative ratings. Then my database has only one field designed for this function, and the type is long character. Combine three comments into an array:
array(
'a' => 0, //0 positive reviews
'b' => 0, //0 neutral reviews
'c' => 0 //0 negative comments
)
After converting it with the serialize function, it is: a:3:{s:1:"a";i:0 ;s:1:"b";i:0;s:1:"c";i:0;}, then the database exists. Don't forget to use the unserialize function to convert it into an array when taking it out.
The above is the correct way to use PHP serialized arrays.