在文件中保存PHP的数组哪种方法更好?
对php来说将一些配置信息数组保存到文件中是司空见惯的情况,就我目前所知,有以下几种保存方法
- 直接用
var_export
写成return array(...);
的php代码,然后读取的时候用require
解析即可。 - 使用
serialize
函数序列化一个数组,然后读取的时候用unserialize
反序列化 - 使用
json_encode
序列化一个数组,然后读取的时候用json_decode
反序列化 - 使用其它的文件格式,比如
yaml
或者ini
有人比较过这几种方法的优劣吗?哪种方法更好,或者说它们适用的场景有和不同?
回复内容:
对php来说将一些配置信息数组保存到文件中是司空见惯的情况,就我目前所知,有以下几种保存方法
- 直接用
var_export
写成return array(...);
的php代码,然后读取的时候用require
解析即可。 - 使用
serialize
函数序列化一个数组,然后读取的时候用unserialize
反序列化 - 使用
json_encode
序列化一个数组,然后读取的时候用json_decode
反序列化 - 使用其它的文件格式,比如
yaml
或者ini
有人比较过这几种方法的优劣吗?哪种方法更好,或者说它们适用的场景有和不同?
如果你的数组是只是给php项目用,使用第一种方法是最好的最方便的,我一直用它
如果你的数据可能不仅仅被php所用,那最好使用通用性更高的json格式了,当然yaml,xml也行,但总归不如json_encode方便
serialize我还真的很少在保存数组的时候用到
PS:不好意思刚才没看到你是说在配置文件的使用。如果是配置文件的话,一定要注意你的php代码会不会有可能被加密,如果有这种可能性最好是使用json或者yml或者ini等其他格式方便配置文件的修改
我觉得第一种方式不是很好,这样可能会造成执行任意PHP代码的漏洞。
推荐用JSON,可读性好(和序列化相比);可读可写(php没有内置函数来写ini);通用性好,其他语言也可以比较轻松地访问。
一般来说,用 ini
是最好不过的了(人工可配),其次是serialize
(系统自动生成)
我一般都是用JSON,跨平台,而且易阅读。
如果需要较大压缩,可选用苹果的binary plist格式,缺点是肉眼不可读,需要引入CFPropertyList。
一般需求用json或xml就可以了。CFPropertyList也可以用来写入或解析xml
如果仅供PHP使用,我还是比较喜欢直接用PHP数组。
serialize
必须是这个方式,因为很简单,只需要序列化就可以了。相对于josn会更方便(如果你是在PHP项目中用的话,而无需其他非PHP项目下使用)推荐serialize。
还是用json吧。可读性强。体积小。解析速度快。跨语言。
XML虽然也拥有强大的可读性。但是比起JSON来说,太过庞大,解析速度也是问题。
YAML可读性一般,较复杂的内容肉眼看起来那就是一堆天书了。但毋庸置疑文件体积将是最小的。
除var_export外。你需要考虑文件直接被浏览器打开的问题。
最终还是力荐JSON,json_encode($content,15) 。推荐使用第二个参数。json_decode($data,true)可轻易的将object转换为array类型。方便操作。
看需求.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
