数组有棘手的重复

时间:2014-11-13 20:37:12

标签: php arrays ini

我使用PHP函数parse_ini_file()从我拥有的INI文件构建数组结构,但我有点被迫创建自定义解析器,因为本机函数不支持嵌套文件,也不解析它档案评论。

虽然真正的INI文件不支持复杂的层次结构,但是通过这样的功能,我可以轻松地操纵我拥有的其他信息(或者我希望如此)。

并且在文件中包含注释的可能性并不是真正的要求,但如果我允许通过GUI编辑INI文件,如果我无法访问它们,那么生成的文件就没有多大意义

这个“不是必需的要求”是我无法解决的问题。

是的......我正在this Gist发布课程(以及支持界面),因为它有点大。

  

不要太难判断方法 Reader :: process(),因为这不是真正的类。真正的一个涉及整个流阅读器,如果发布在这里会使事情复杂化。但是,该片段是有效的替代品。

我用于测试目的的INI文件是这样的:

[info]
; Comment Section 1: author
author="Bruno Augusto"
; Comment Section 1: copyright
copyright="MIT"

[descriptor]
; Comment Section 2: name
name="Item Name"
; Comment Section 2: description
description="Item Description"
; Comment Section 2: Line 1
; Comment Section 2, Line 2 (version)
version=1.0

[contents]
; Comment Section 3: Line 1
; Comment Section 3, Line 2 (files)
files[] = config.ini
files[] = folder/file1.php
files[] = folder/folder2/file2.php
files[] = folder/folder2/folder3/file3.php
files[] = folder/folder2/folder3/file4.php

[hierarchyTest]
; Comment Section 4: levels
levels[first\second\third] = "foo"
levels[first\second\third] = "anotherone"
levels[first\second\third] = "onemore"
levels[first\second\third\fourth] = "bar"
levels[first\second\third\fourth] = "baaz"

[hierarchyTest2]
; Comment Section 5: Line 1
; Comment Section 5, Line 2 (levels2)
levels2[first.second.third] = "baaz"

使用方法就像使用INI文件的路径实例化Reader类并调用 Reader :: read()方法一样简单。我们都是大男孩,我想我可以压制这个。 :P

从第68行开始处理错误评论,并将其添加到行81 *中的最终结构中。

问题是目前所有值都是在每次迭代后累积的。值得责备的是行69中使用的array_merge()。删除它:

$comments = $line;

几乎一切都运行正常,但多行注释只保留直接赋值所期望的最后一行。

最初,我认为我可以在使用它之后简单地清空数组,所以我在行84中添加了这样的指令:

$comments = array();

这是奇怪的。它适用于几乎所有条目,但名为内容 hierarchyTest 的部分没有添加任何评论。

我已经解决了这个问题,这是因为层次结构解析,从第92行开始,但我不知道如何解决这个问题。

这种隔离是如此真实,如果我再添加一个条目到最后一个部分(hierarchyTest2),格式为:

levels2[first.second.third] = "baazaaa"

它的评论也消失了。

0 个答案:

没有答案
相关问题