Home  >  Article  >  Backend Development  >  应用PHP序列化数组技巧_PHP教程

应用PHP序列化数组技巧_PHP教程

WBOY
WBOYOriginal
2016-07-15 13:34:071046browse

serialize --------- 将数组格式化成有序的字符串
unserialize ----- 将数组还原成数组

PHP序列化数组测试实例:

$test = array("a"=>0,"b"=>0,"c"=>0);
$test2 = '';
$test2=serialize($test);
echo $test2;
echo "


";
print_r(unserialize($test2));

PHP序列化数组用途:

在我个人经验主要是处理数组传递,数组存库操作。

例如我有一个数组需要传递给下一个页面,如果你不想使用seesion/cookie,那么你就可以利用该函数,传递,然后还原。
例如我在做网址目录的时候,其中有个 评分,分为好评,中评,差评。那么我的数据库给该功能设计的字段就一个,类型是长字符型。将三个评论组合成数组:

array(
'a' => 0, //好评0个
'b' => 0, //中评0个
'c' => 0 //差评0个
)

将其用serialize函数转换后就是:a:3:{s:1:"a";i:0;s:1:"b";i:0;s:1:"c";i:0;},然后存在数据库。取出的时候别忘记用unserialize函数转换成数组使用。

以上就是PHP序列化数组的正确使用方法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446013.htmlTechArticleserialize --------- 将数组格式化成有序的字符串 unserialize ----- 将数组还原成数组 PHP序列化数组测试实例: $test = array(a=0,b=0,c=0); $test2 = ''; $t...
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