Array serialization using PHP

Source: Internet
Author: User

Serialize --------- format the array into an ordered string
Unserialize ----- restore an array to an array

PHP serialization array test instance:

$ Test = array ("a" => 0, "B" => 0, "c" => 0 );
$ Test2 = '';
$ Test2 = serialize ($ test );
Echo $ test2;
Echo "Print_r (unserialize ($ test2 ));

PHP serialization array usage:

In my personal experience, I mainly deal with array transfer and array inventory operations.

For example, I have an array that needs to be passed to the next page. If you do not want to use seesion/cookie, you can use this function to pass and restore it.
For example, when I make a website directory, there is a score, which can be divided into high praise, medium rating, and poor rating. Then, my database designs one field for this function, and the type is long standby type. Combine the three comments into an array:

Array (
'A' => 0, // 0
'B' => 0, // 0
'C' => 0 // 0 negative ratings
)

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;}, and then the database exists. Do not forget to use the unserialize function to convert it to an array.

The above is the correct method for using PHP serialized arrays.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.