Home  >  Article  >  Backend Development  >  How to deserialize arrays and objects in php

How to deserialize arrays and objects in php

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-08-06 10:33:222663browse

In the previous article, we learned about the method of serializing objects or arrays. If you need it, please read "How to serialize arrays and objects in php". This time we will introduce to you the method of deserializing objects or arrays. You can refer to it if necessary.

Before we start this article, let’s first understand what deserialization is.

Deserialization is the process of restoring byte streams (binary strings) into arrays and objects.

After we understand the meaning of deserialization, it is easy for us to understand what this article needs to do. We need to restore the serialized objects and arrays in the previous article.

Let’s look at a small example first, which is to restore the serialized array in the previous article.

The example in the previous article is

<?php    
$sites = array(&#39;Google&#39;, &#39;360&#39;, &#39;Facebook&#39;);    
$serialized_data = serialize($sites);    
echo  $serialized_data . PHP_EOL;  
?>

The result is

How to deserialize arrays and objects in php

Then we will restore this byte stream into an array.

<?php
$str = &#39;a:3:{i:0;s:6:"Google";i:1;s:3:"360";i:2;s:8:"Facebook";}&#39;;
$unserialized_data = unserialize($str);
print_r($unserialized_data);
?>

The restored result is

How to deserialize arrays and objects in php

Okay, let’s restore the result now. Let's explain it carefully. Such a long string of characters becomes an array after passing a function. Although we don't know what happened in this long sequence? How it went from a long string of numbers and letters to the array we are familiar with.

But we can learn this function.

The unserialize() function is used to deserialize the object or array serialized by the serialize() function and return the original object structure.

Then let’s take a look at the syntax structure of this function.

mixed unserialize (需要进行反序列化操作的字符串)

What needs to be noted is the return value of this function. Let’s take a closer look.

This function returns the converted value, which can be integer, float, string, array or object. If the passed string is not deserializable, FALSE is returned and an E_NOTICE is generated.

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 deserialize arrays and objects in php. For more information, please follow other related articles on the PHP Chinese website!

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