Home  >  Article  >  Backend Development  >  Example of php serialized array

Example of php serialized array

WBOY
WBOYOriginal
2016-07-25 08:56:48954browse
  1. //Serialized array
  2. $test = array("a"=>0,"b"=>0,"c"=>0);
  3. $test2 = '';
  4. $test2=serialize($test);
  5. echo $test2;
  6. echo "
    ";
  7. print_r(unserialize($test2));
Copy code

2, PHP serialization Array usage: It mainly handles array transfer and array storage operations.

For example, there is an array that needs to be passed to the next page. If you do not want to use seesion/cookie, you can use the above function to serialize and pass it, and then restore it. For example, if you are making a website directory program, there will be a rating, which is divided into good reviews, medium reviews, and bad reviews. Then, the database has only one field designed for this function, and the type is long character. Combine three comments into an array:

  1. array(
  2. 'a' => 0, //0 positive reviews
  3. 'b' => 0, //0 neutral reviews
  4. 'c' => 0 //0 negative reviews
  5. )
Copy the code

and convert it with the serialize function: 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.

Serialization of arrays is often used. It is recommended that everyone has a firm grasp of it, especially the usage of the serialize and unserialize functions, but also a flexible grasp.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn