在PHP项目中使用YAML

Test fixtures, configuration files, and log files all need to be both human and machine readable. YAML (YAML Ain’t Markup Language) is a less-verbose data serialization format than XML and has become a popular format among software developers mainly because it is human-readable. YAML files are simply text files that contain data written according to YAML syntax rules and usually have a .yml file extension. In this article, you will learn the basics of YAML and how you can integrate a PHP YAML parser into your projects.

测试装置,配置文件和日志文件都必须是人类和机器可读的。 与XML相比,YAML(YAML不是标记语言)是一种不太冗长的数据序列化格式,并且由于它易于阅读,因此已成为软件开发人员中的流行格式。 YAML文件是简单的文本文件,其中包含根据YAML语法规则编写的数据,并且通常具有.yml文件扩展名。 在本文中,您将学习YAML的基础知识以及如何将PHP YAML解析器集成到项目中。

了解YAML语法 (Understanding YAML Syntax)

YAML supports advanced features like references and custom data types, but as a PHP developer, most of the time you’ll be interested in how YAML represents enumerated arrays (sequences in YAML terminology) and associative arrays (mappings).

YAML支持引用和自定义数据类型等高级功能,但是作为PHP开发人员,大多数时候您会对YAML如何表示枚举数组(YAML术语中的序列)和关联数组(映射)感兴趣。

The following is how to represent an enumerated array in YAML:

以下是如何在YAML中表示枚举数组:

- 2
- "William O'Neil"
- false

Each element of the array is presented after a hyphen and a space. It’s syntax for representing values is similar to that of PHP (quoting strings, etc.)

数组的每个元素都在连字符和空格之后显示。 表示值的语法与PHP相似(引用字符串等)

The above is equivalent to the following PHP:

以上等同于以下PHP:

<?php
array(2, "William O'Neil", false);

Generally, each element will appear on it’s own line in YAML, but enumerated arrays can also be expressed on a single line using brackets:

通常,每个元素都将在YAML中单独显示在一行上,但是枚举数组也可以使用括号在一行上表示:

[ 2, "William O'Neil", false ]

The following code shows how to represent an associative array in YAML:

以下代码显示了如何在YAML中表示关联数组:

id:       2
name:     "William O'Neil"
isActive: false

First the element’s key is stated followed by a colon and one or more spaces, and then the value is stated. Having just one space after the colon is sufficient, but you can use more spaces for the sake of better readability if you like.

首先说明元素的键,然后说明冒号和一个或多个空格,然后说明值。 在冒号后面仅留一个空格就足够了,但是如果您愿意,可以使用更多的空格以提高可读性。

The equivalent PHP array of the above YAML is:

上述YAML的等效PHP数组是:

<?php
array("id" => 2, "name" => "William O'Neil", "isActive" => false);

And similar to enumerated arrays, you can express associative arrays on a single line using braces:

与枚举数组类似,您可以使用花括号在一行上表示关联数组:

{ id: 2, name: "William O'Neil”, isActive: false }

With one or more spaces for indentation, you can represent a multi-dimensional array like so:

使用一个或多个缩进空间,您可以表示一个多维数组,如下所示:

author:
  0: { id: 1, name: "Brad Taylor", isActive: true }
  1: { id: 2, name: "William O'Neil", isActive: false }

Note that although the second level arrays are enumerated arrays, I have used the syntax for mappings (colons) instead of the syntax for sequences (hyphens) for clarity.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值