🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# php序列化和反序列化解析 serialize() ---> 函数用于序列化对象或数组,并返回一个字符串。如下: ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php $sites <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'t1'</span><span class="token1">,</span> <span class="token4">'tt2'</span><span class="token1">,</span> <span class="token4">'ttt3'</span><span class="token1">)</span><span class="token1">;</span> $serialized_data <span class="token">=</span> <span class="token3">serialize</span><span class="token1">(</span>$sites<span class="token1">)</span><span class="token1">;</span> echo $serialized_data<span class="token1">;</span> <span class="token">?</span><span class="token">></span> ``` ``` 输出结果 a:3:{i:0;s:2:"t1";i:1;s:3:"tt2";i:2;s:4:"ttt3";} 各个字符的意义 ---> `o表示对象,a表示数组,s表示字符,i表示数字` a:3 表示有三个数组 i:0,表示第一个数组,s:2:"t1",表示第一个数组是字符,2表示有两个字符,为"t1" i:1,表示第二个数组,s:3:"tt2",表示第二个数组是字符,3表示有三个字符,为"tt2" i:2,表示第三个数组,s:4:"ttt3",表示第三个数组是字符,4表示有三个字符,为"ttt3" unserialize() ---> 函数用于将通过 serialize()函数序列化后的对象或数组进行反序列化,并返回原始的对象结构,如: ``` <pre class="calibre13">``` <span class="token"><</span><span class="token">?</span>php $sites <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'t1'</span><span class="token1">,</span> <span class="token4">'tt2'</span><span class="token1">,</span> <span class="token4">'ttt3'</span><span class="token1">)</span><span class="token1">;</span> $serialized_data <span class="token">=</span> <span class="token3">serialize</span><span class="token1">(</span>$sites<span class="token1">)</span><span class="token1">;</span> #echo $serialized_data<span class="token1">;</span> $unserialized_data <span class="token">=</span> <span class="token3">unserialize</span><span class="token1">(</span>$serialized_data<span class="token1">)</span><span class="token1">;</span> <span class="token3">print_r</span><span class="token1">(</span>$unserialized_data<span class="token1">)</span><span class="token1">;</span> <span class="token">?</span><span class="token">></span> ``` ``` 结果如下: ``` <pre class="calibre14">``` Array <span class="token1">(</span> <span class="token1">[</span><span class="token5">0</span><span class="token1">]</span> <span class="token">=</span><span class="token">></span> t1 <span class="token1">[</span><span class="token5">1</span><span class="token1">]</span> <span class="token">=</span><span class="token">></span> tt2 <span class="token1">[</span><span class="token5">2</span><span class="token1">]</span> <span class="token">=</span><span class="token">></span> ttt3 <span class="token1">)</span> ``` ```