PHP serialization array development an idea of the album module-PHP development

Source: Internet
Author: User
A photo album function module is being developed recently. The process is probably to generate an array of batch uploaded image data and then store the array in a field of the data table, when reading images, traverse the array to browse multiple images. However, arrays cannot be read and written directly to the database. Therefore, you must convert the array into strings before saving it to the database. The string retrieved from the database must be restored to an array for traversal and reading. Previously, I thought that such a process of mutual conversion would need to be processed by writing a very complex function, and later I proved to be a multi-filter. Remember that PHP has a built-in function that is currently developing an album function module. The process is probably like: generate an array of batch uploaded image data, and then save the array to a field in the data table, when reading images, traverse the array to browse multiple images.


However, arrays cannot be read and written directly to the database. Therefore, you must convert the array into strings before saving it to the database. The string retrieved from the database must be restored to an array for traversal and reading. Previously, I thought that such a process of mutual conversion would need to be processed by writing a very complex function, and later I proved to be a multi-filter. Remember that PHP has a built-in function serialize () that can process Object serialization. After turning over the php manual, the serialize () and unserialize () can also serialize arrays! Now let's take a look at its usage:

Serialize () -- returns a string that contains the byte stream that represents value and can be stored anywhere. This facilitates storing or passing PHP values without losing their types and structures.


Unserialize () -- operate a single serialized variable and convert it back to the PHP value. The returned value is the converted value, which can be integer, float, string, array, or object. If the passed string cannot be deserialized, FALSE is returned.

Below are my debugging records:

 '1, jpg ', '1' => '2, jpg', '2' => '3, jpg '); $ picarray = serialize ($ picarray ); // serialize the array and store it to the database print

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.