Php 构建嵌套数组
我有一个大纲格式的文本文档,最多可以有6个级别Php 构建嵌套数组,php,arrays,recursion,tree,Php,Arrays,Recursion,Tree,我有一个大纲格式的文本文档,最多可以有6个级别 .step1 -- step 1. text ..step2 -- step 1. A. text ..step2 -- step 1. B. text ..step2 -- step 1. C. text ..step2 -- step 1. D. text ..step2 -- step 1. E. text .step1 -- step 2. text ..step2 -- step 2. A. text ...step3 -- step 2
.step1 -- step 1. text
..step2 -- step 1. A. text
..step2 -- step 1. B. text
..step2 -- step 1. C. text
..step2 -- step 1. D. text
..step2 -- step 1. E. text
.step1 -- step 2. text
..step2 -- step 2. A. text
...step3 -- step 2. A. (1) text
...step3 -- step 2. A. (2) text
.step1 -- step 3. text
我正在用正则表达式解析文本文档,并构建一个如下结构的数组
$contentsArray structure
'level' INT - the step level
'type' STRING - the type of tag, note, warning, step1, step2
'line' INT - the line number in the file
'text' STRING - the text
------ SAMPLE ARRAY -------
[0] => Array
(
[level] => 1
[type] => step1
[line] => 8
[text] => Step 1. text
)
[1] => Array
(
[level] => 2
[type] => step2
[line] => 10
[text] => Step 1. A. text
)
[2] => Array
(
[level] => 2
[type] => step2
[line] => 12
[text] => Step 1.B. text
)
[3] => Array
(
[level] => 2
[type] => step2
[line] => 14
[text] => Step 1. C. text.
)
[4] => Array
(
[level] => 2
[type] => step2
[line] => 16
[text] => Step 1. D. text.
)
[5] => Array
(
[level] => 2
[type] => step2
[line] => 18
[text] => Step 1. E. text.
)
[6] => Array
(
[level] => 1
[type] => step1
[line] => 20
[text] => Step 2. text
)
[7] => Array
(
[level] => 2
[type] => step2
[line] => 22
[text] => Step 2. A. Text.
)
[8] => Array
(
[level] => 3
[type] => step3
[line] => 26
[text] => Step 2. A. (1) Text.
)
[9] => Array
(
[level] => 3
[type] => step3
[line] => 28
[text] => Step 3. A. (2) Text.
)
[10] => Array
(
[level] => 1
[type] => step1
[line] => 30
[text] => Step 3. Text
)
最终目标是将其转换为嵌套的XML文档
<step1>Step 1. text
<step2>Step 1. A. text</step2>
<step2>Step 1. B. text</step2>
<step2>Step 1. C. text</step2>
<step2>Step 1. D. text</step2>
<step2>Step 1. E. text</step2>
</step1>
<step1>Step 2. text
<step2>Step 2. A. text
<step3>Step 2. A. (1) text</step3>
<step3>Step 2. A. (2) text</step3>
</step2>
</step1>
<step1>Step 3. text
</step1>
我需要的是一种方法的帮助,该方法可以循环遍历我构建的数组,并使用级别
值来计算最终数组中的嵌套。到目前为止,我的尝试都是徒劳的。我觉得有一种递归或迭代器的方法可以做到这一点,但这不是我的强项
谢谢你的帮助,我希望这个问题足够清楚
更新问题我发现我问这个问题做得很差,所以我做了一些编辑。你的例子不一致。假设最后一个
…步骤3
是一个错误,这将执行您想要的操作。它是用perl编写的,但是应该很容易翻译
sub main {
my @labels;
while (<>) {
if (/^(\.+)(.*)$/) {
my $level = length($1);
print "</" . pop(@labels) . ">\n" while $level <= scalar(@labels);
die "Bad input" unless $level == 1 + scalar(@labels);
print "<" . $2 . ">\n";
push @labels, $2
}
}
print "</" . pop(@labels) . ">\n" while (@labels);
}
main;
sub-main{
我的@标签;
而(){
如果(/^(\.+)(.*)$/){
我的$level=长度($1);
打印“\n”而$level您的示例不一致。假设最后一个…步骤3
是一个错误,这将满足您的需要。它是perl语言,但应该很容易翻译
sub main {
my @labels;
while (<>) {
if (/^(\.+)(.*)$/) {
my $level = length($1);
print "</" . pop(@labels) . ">\n" while $level <= scalar(@labels);
die "Bad input" unless $level == 1 + scalar(@labels);
print "<" . $2 . ">\n";
push @labels, $2
}
}
print "</" . pop(@labels) . ">\n" while (@labels);
}
main;
sub-main{
我的@标签;
而(){
如果(/^(\.+)(.*)$/){
我的$level=长度($1);
打印“\n”而$level这是一个PHP实现,用于在变量$input
中输入文本时:
$stack = [];
foreach(explode("\n", $input . "\n.") as $line) {
$line = trim($line);
$type = ltrim($line, ".");
$dots = strlen($line) - strlen($type);
if (!$dots || $dots > count($stack) + 1) throw new Exception("Bad input format");
while($dots <= count($stack))
$xml[] = str_repeat(" ", count($stack)-1) . array_pop($stack);
$xml[] = str_repeat(" ", count($stack)) . "<$type>";
$stack[] = "</$type>";
}
$xml = implode("\n", array_slice($xml, 0, -1));
echo $xml;
$stack=[];
foreach(分解(“\n”,$input.“\n.”)为$line){
$line=修剪($line);
$type=ltrim($line,“.”);
$dots=strlen($line)-strlen($type);
如果(!$dots | |$dots>count($stack)+1)抛出新异常(“输入格式错误”);
而($dots这里是PHP实现,用于在变量$input
中输入文本时:
$stack = [];
foreach(explode("\n", $input . "\n.") as $line) {
$line = trim($line);
$type = ltrim($line, ".");
$dots = strlen($line) - strlen($type);
if (!$dots || $dots > count($stack) + 1) throw new Exception("Bad input format");
while($dots <= count($stack))
$xml[] = str_repeat(" ", count($stack)-1) . array_pop($stack);
$xml[] = str_repeat(" ", count($stack)) . "<$type>";
$stack[] = "</$type>";
}
$xml = implode("\n", array_slice($xml, 0, -1));
echo $xml;
$stack=[];
foreach(分解(“\n”,$input.“\n.”)为$line){
$line=修剪($line);
$type=ltrim($line,“.”);
$dots=strlen($line)-strlen($type);
如果(!$dots | |$dots>count($stack)+1)抛出新异常(“输入格式错误”);
如果你在使用PHP,你应该在问题上加上“PHP”标记。我是用PHP做的,但对我来说,这更像是一个技术问题。你描述了一个具有4个属性的关联数组,但是输入中的“文本”部分在哪里,为什么数组只有“文本”属性,而不是其他3个?如果你使用PHP,你应该用“PHP”标记问题。我用PHP做这件事,但对我来说,这更多的是一个技术问题。你描述了一个具有4个属性的关联数组,但是输入中的“文本”部分在哪里,为什么你的数组只有一个“文本”属性而没有其他3个属性?