将嵌套的父/子元素数组转换为大纲编号格式


Convert nested parent/child element array into an outline numbered format

为了能够搜索和索引/排序数组

我有一个大的多维数组,我正在尝试解析它。现在,我只是简单地使用CakePHP外壳来实现这一点,但任何方法都足够了。我需要做的是获取父/子/更多子的数组,并创建一个带有相关"大纲编号"的id数组

例如:

1
1.1
1.2
1.2.1
1.2.2
1.2.3

等等。

以下是我的"输入阵列"示例:

Array
(
    [0] => Array
        (
            [AsapStructure] => Array
                (
                    [id] => 1
                    [lft] => 30267
                    [rght] => 32774
                    [parent_id] => 
                    [wbs] => 
                )
            [children] => Array
                (
                    [0] => Array
                        (
                            [AsapStructure] => Array
                                (
                                    [id] => 2
                                    [lft] => 30268
                                    [rght] => 30773
                                    [parent_id] => 1
                                    [wbs] => 1
                                )
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [AsapStructure] => Array
                                                (
                                                    [id] => 3
                                                    [lft] => 30269
                                                    [rght] => 30382
                                                    [parent_id] => 2
                                                    [wbs] => 1.1
                                                )

我设想的是拥有一个简单的自递归解析机制。我开发的一个例子如下:

<?php
    var $structIndex = array();
    private function __parseStruct( $toParse,$prefix = null ) {
        $iterator = 0;
        if ( $prefix )
            $prefix = $prefix . '.';
        foreach( $toParse as $datum ) {
            $iterator++;
            if ( $datum['AsapStructure']['id'] == 1 )
                $this->structIndex[ 1 ] = NULL;
            else            
                $this->structIndex[ $datum['AsapStructure']['id'] ] = $prefix . $iterator  ;
            $subiterator = 0;
            foreach( $datum['children'] as $key => $data ) {
                $subiterator++;
                $this->structIndex[ $data['AsapStructure']['id'] ] = $prefix . $subiterator;
                if ( ! empty( $datum['children'] ) ) {
                    $this->__parseStruct( $datum['children'],  $subiterator );
                }
            }
        }
        return $this->structIndex;
    }
?>

我所做的是,我调用这个$this->__parseStruct( $data )函数并将数组传递给它。然后,该函数在数组上循环,为嵌套的子数组调用该函数。我的这一部分工作正常,只是我似乎无法获得创建"索引"的正确逻辑,即1.1、1.2、1.2.1等等。

所以我的目标输出是:

$array[ $rowid ] = [ 1.1 / 1.2 / 1.2.1 / 1.2.2 ] or any combination as such.

非常感谢您的帮助。

我走在了正确的轨道上,我只是增加了一个不必要的额外步骤。

private function __parseStruct( $toParse,$prefix = null ) {
    $iterator = 0;
    if ( $prefix )
        $prefix = $prefix . '.';
    foreach( $toParse as $datum ) {
        $iterator++;
        if ( $datum['AsapStructure']['id'] == 1 )
            $this->structIndex[ 1 ] = NULL;
        else            
            $this->structIndex[ $datum['AsapStructure']['id'] ] = $prefix . $iterator  ;

        if ( ! empty( $datum['children'] ) ) {
            $this->__parseStruct( $datum['children'],  $prefix.$iterator );
        }

    }
    return $this->structIndex;
}

这解决了问题。然而,由于我的数组的性质,我有必要首先通过执行:$this->__parseStruct( $data[0]['children'] );来调用函数,否则我会得到1。在生成的数组中为每个数据元素加前缀。