根据字符串开头的连续符号数创建分层多维数组

Create hierarchical multidimensional array based on number of consecutive symbols at start of strings

我不确定标题是否正确,因为它很难解释。 我想根据每个数组的主题标签计数将一维数组转换为多维数组。

所以基本上我想要这个。

[
    0 => "# Grandparent #1"
    1 => "# Grandparent #2"
    2 => "## Parent #2-1"
    3 => "### Child #2-1-1"
    4 => "## Parent #2-2"
    5 => "### Child #2-2-1"
    6 => "### Child #2-2-2"
    7 => "## Parent #2-3"
    8 => "## Parent #2-4"
    9 => "# Grandparent #3"
    10 => "## Parent #3-1"
]

像这样

[
    [
        'name' => 'Grandparent #1'
    ],
    [
        'name' => 'Grandparent #2',
        'children' => [
            [
                'name' => 'Parent #2-1',
                'children' => [
                    [
                        'name' => 'Child #2-1-1',
                    ]
                ]
            ],
            [
                'name' => 'Parent #2-2',
                'children' => [
                    [
                        'name' => 'Child #2-2-1'
                    ],
                    [
                        'name' => 'Child #2-2-2'
                    ]
                ]
            ],
            [
                'name' => 'Parent #2-3',
            ],
            [ 
                'name' => 'Parent #2-4',
            ],
        ],
    ],
    [
        'name' 'Grandparent #3',
        'children' => [
            [
                'name' => 'Parent #3-1'
            ]
        ]
    ]
]

我的代码:
数据集是最小的可重现示例。
上一个之后也可以有无限个#
第二个标签 (#2-1-1) 用于提供清晰度,而不是问题的一部分。

$array = [
    "# Grandparent #1",
    "# Grandparent #2",
    "## Parent #2-1",
    "### Child #2-1-1",
    "## Parent #2-2",
    "### Child #2-2-1",
    "### Child #2-2-2",
    "## Parent #2-3",
    "## Parent #2-4",
    "# Grandparent #3",
    "## Parent #3-1",
];
function structure($lines, $target = 1) {
    $data = [];
    $parent = 0;
    $i = 0;
    foreach($lines as $line) {
        $current = strlen(preg_split('/\s+/', $line)[0]);
        if ($current == $target) {
            $parent = $i;
            $data[$parent]['name'] = $line;
        }
        if ($current != $target) {
            $data[$parent]['children'][] = $line;
        }
        $i++;
    }
    // I tried placing structure function here again but it gives me errors
    // structure($data[$parent]['children'], $target + 1);
    return $data;
}

$data = structure($array);

我让祖父母工作,但我似乎无法让它超越其余部分。我已经尝试将循环放在一边以使其搜索另一个 children 但它只会无限地 运行 。而且我不能将 foreach 放在 foreach 中,依此类推,因为主题标签的数量可以是任意长度。

我没有太多时间研究这个,但这是我想出的...

  1. 遍历行数组
  2. 将散列符号序列与成员名称分开
  3. 测量哈希符号字符串的长度并与当前级别进行比较
  4. 对于给定级别上“父级”的每个实例,创建一个引用变量并将其推入输出数组(树)
  5. 每次在给定关卡遇到新的“父级”时,必须销毁旧引用并声明新引用
  6. 当需要递归时,仅将直接后代传递给递归调用
  7. 需要在循环结束时有条件地调用递归调用,以便处理未处理的后代

代码:(Demo)

function recurse(array $lines, int $level = 1): array
{
    $tree = [];
    $directDescendants = [];
    foreach ($lines as $line) {
        [$hashes, $name] = explode(' ', $line, 2);
        $depth = strlen($hashes);
        if ($depth === $level) {
            if ($directDescendants) {
                $parent['children'] = recurse($directDescendants, $level + 1);
            }
            $directDescendants = [];      // reset collection of direct descendants
            unset($parent);               // destroy reference
            $parent = ['name' => $name];  // name the member
            $tree[] = &$parent;           // push the reference variable into the tree
        } elseif ($depth > $level) {
            $directDescendants[] = $line; // collect only direct descendants of current level parent member
        }
    }
    if ($directDescendants) {
        $parent['children'] = recurse($directDescendants, $level + 1);
    }
    return $tree;
}

var_export(recurse($array));