Table of Contents
回复内容:
Home Backend Development PHP Tutorial 在文件中保存PHP的数组哪种方法更好?

在文件中保存PHP的数组哪种方法更好?

Jun 06, 2016 pm 08:51 PM
php array

对php来说将一些配置信息数组保存到文件中是司空见惯的情况,就我目前所知,有以下几种保存方法

  1. 直接用var_export写成return array(...);的php代码,然后读取的时候用require解析即可。
  2. 使用serialize函数序列化一个数组,然后读取的时候用unserialize反序列化
  3. 使用json_encode序列化一个数组,然后读取的时候用json_decode反序列化
  4. 使用其它的文件格式,比如yaml或者ini

有人比较过这几种方法的优劣吗?哪种方法更好,或者说它们适用的场景有和不同?

回复内容:

对php来说将一些配置信息数组保存到文件中是司空见惯的情况,就我目前所知,有以下几种保存方法

  1. 直接用var_export写成return array(...);的php代码,然后读取的时候用require解析即可。
  2. 使用serialize函数序列化一个数组,然后读取的时候用unserialize反序列化
  3. 使用json_encode序列化一个数组,然后读取的时候用json_decode反序列化
  4. 使用其它的文件格式,比如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类型。方便操作。

看需求.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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 Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

See all articles