In the previous article, we learned about how to set variable types. If necessary, please read "How to use functions to set variable types in php". This time we will introduce to you the method of serializing objects or arrays. You can refer to it if necessary.
This time we take a look at serialization, but do you know what serialization is?
Serialization is the process of converting an object's state information into a form that can be stored or transmitted. During serialization, the object writes its current state to temporary or persistent storage. Later, you can recreate the object by reading or deserializing the object's state from storage.
After we understand the knowledge of serialization, let us quickly learn how to serialize objects or arrays.
First let’s look at a little chestnut.
<?php $sites = array('Google', '360', 'Facebook'); $serialized_data = serialize($sites); echo $serialized_data . PHP_EOL; ?>
The result of this example is
What did we do? How did this result become like this? What does this long string of codes mean? I know every word in the name, why don’t I know them all together?
Let’s explain it in detail. We originally had an array, but after some operations, our array turned into a long string of codes that we didn’t recognize. Since we can't explain it anymore, let's take a look at this function.
serialize() function is used to serialize an object or array and return a string. After this function serializes the object, it can be easily passed to other places that need it, and its type and structure will not change.
Then let’s take a look at the syntax format of this function.
string serialize(要序列化的对象或数组)
It seems that the process we don't know is the serialization process performed by this function, and then the object or array is turned into a long string.
It should be noted that if you want to change the serialized string back to a PHP value, you can use the <strong>unserialize()</strong>
function.
That’s all. If you want to know anything else, you can click here. → →php video tutorial
The above is the detailed content of How to serialize arrays and objects in php. For more information, please follow other related articles on the PHP Chinese website!