将数组转换成字符串存储有没有更好的方法或函数(字符串长度越小越好)?

WBOY
Release: 2016-06-06 20:11:34
Original
916 people have browsed it

因为要将数组信息保存到数据库中,我试过用serializejson_encodeserializejson_encode转换来得字符串长度要少1/3,但是想请教PHP中还有没有别的函数方法可以转换出来更少长度的字符串

回复内容:

因为要将数组信息保存到数据库中,我试过用serializejson_encodeserializejson_encode转换来得字符串长度要少1/3,但是想请教PHP中还有没有别的函数方法可以转换出来更少长度的字符串

没必要吧,json_encode其实已经很好了,尤其是考虑数据可能被前端直接调用的时候。如果你的数据库空间不足可以买块硬盘,这是最好的建议。
你压缩的越厉害就需要更大的运算量,解压的时候也就更麻烦,节约空间丧失性能根本就划不来。

你可以自己搜索一下字符串压缩算法,比如 http://www.jb51.net/article/46144.htm

一般来说,除非特殊需求,正常情况下CPU时间比磁盘空间贵得多,所以无需为了磁盘空间而去浪费CPU时间。
压缩、解压缩字符串都很耗时,并不值得。

主要是看需求,你的字符串压缩之后用于实现什么内容,如果是和其他部分沟通的话,那么用json是比较好的实现方法。如果是用来储存,那么就另算了。

implode(separator,array)

MessagePack 是一个高效的二进制序列化格式。它让你像JSON一样可以在各种语言之间交换数据。但是它比JSON更快、更小

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!