今天,在做一个关于PING服务的东西,由于不想把ping服务地址放进数据库中,那样不好,ping服务本来不是一个效率不高的事,再放进数据库中,不但更加降低了程序的效率,还给服务器带来不必要的损耗,所以,想办法所ping服务地址用数组的方式放进了.conf文件中,才开始的设计思路是通过生成的php代码放到.php文件中,然后再把.php文件载入进来,后来,发现操作起来不太方便,所以,决定使用serialize()函数和unserialize()函数,这两个函数的用法真是绝配,一个是进行序列化存储,另一个则是进行序列化恢复,方便极了,函数用法代码分享:

1
2
3
4
5
6
7
8
9
10
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000088">$array</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$array</span><span style="color: #009900">[</span><span style="color: #0000ff">'key'</span><span style="color: #009900">]</span> <span style="color: #339933">=</span> <span style="color: #0000ff">'website'</span><span style="color: #339933">;</span>
<span style="color: #000088">$array</span><span style="color: #009900">[</span><span style="color: #0000ff">'value'</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #0000ff">'www.chhua.com'</span><span style="color: #339933">;</span>
<span style="color: #000088">$a</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/serialize" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">serialize</span></a><span style="color: #009900">(</span><span style="color: #000088">$array</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$a</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/unset" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">unset</span></a><span style="color: #009900">(</span><span style="color: #000088">$array</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$a</span> <span style="color: #339933">=</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/unserialize" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">unserialize</span></a><span style="color: #009900">(</span><span style="color: #000088">$a</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<a rel="noopener noreferrer nofollow" href="http://www.php.net/print_r" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">print_r</span></a><span style="color: #009900">(</span><span style="color: #000088">$a</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000000;font-weight: bold">?></span>

没有写太具体的用法,童鞋可以自己思考,然后尝试,还是那句话,我这里说的只是一种思路和方法,具体用法,还是要自己研究。