构建嵌套数组

时间:2017-05-19 19:25:14

标签: 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. 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>

我认为我需要的是构建一个嵌套数组,然后我可以将其转换为XML。我认为这个数组的结构类似于

   [0] => Array
        (
            [level] => 1
            [type] => step1
            [line] => 8
            [text] => Step 1. text
        ), Array
            (
                [level] => 2
                [type] => step2
                [line] => 10
                [text] => Step 1. A. text
            ), Array
            (
                [level] => 2
                [type] => step2
                [line] => 12
                [text] => Step 1.B. text
            ), Array
            (
                [level] => 2
                [type] => step2
                [line] => 14
                [text] => Step 1. C. text.
            ), Array
            (
                [level] => 2
                [type] => step2
                [line] => 16
                [text] => Step 1. D. text.
            ), Array
            (
                [level] => 2
                [type] => step2
                [line] => 18
                [text] => Step 1. E. text.
            )
        )
    [1] => Array
        (
            [level] => 1
            [type] => step1
            [line] => 20
            [text] => Step 2. text
        , Array
            (
                [level] => 2
                [type] => step2
                [line] => 22
                [text] => Step 2. A. Text.
                , Array
                (
                    [level] => 3
                    [type] => step3
                    [line] => 26
                    [text] => Step 2. A. (1) Text.
                ), Array
                (
                    [level] => 3
                    [type] => step3
                    [line] => 28
                    [text] => Step 3. A. (2) Text.
                )
            )
        )

    [2] => Array
        (
            [level] => 1
            [type] => step1
            [line] => 30
            [text] => Step 3. Text
        )

我需要的是一个循环遍历我构建的数组的方法的帮助,并使用level值来计算最终数组中的嵌套。到目前为止,我的尝试一直没有结果。我觉得有一种递归或迭代方式可以做到这一点,但这些并不是我的强项。

感谢您的帮助,我希望这个问题足够明确。

更新问题我看到我在提问时做得很差,所以我做了一些修改。

2 个答案:

答案 0 :(得分:1)

你的例子不一致。假设最后char是一个错误,这将做你想要的。它在perl中,但应该很容易翻译。

...step3

它使用简单的堆栈和处理级别相对于当前堆栈大小的变化。

我会让你弄清楚缩进。您也可以根据堆栈大小执行此操作。

添加

嗯,你已经改变了很多问题。但是使用堆栈和处理级别相对于堆栈长度的基本方法仍然可以正常工作。

答案 1 :(得分:1)

以下是在变量$input中输入文本时的PHP实现:

$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;

eval.in上看到它。