4

我有一个类似的数组:

Array
(
    Array
    (
        [ID] => 1
        [parentcat_ID] => 0
    ),
    Array
    (
        [ID] => 2
        [parentcat_ID] => 0
    ),
    Array
    (
        [ID] => 6
        [parentcat_ID] => 1
    ),
    Array
    (
        [ID] => 7
        [parentcat_ID] => 1
    ),
    Array
    (
        [ID] => 8
        [parentcat_ID] => 6
    ),
    Array
    (
        [ID] => 9
        [parentcat_ID] => 1
    ),
    Array
    (
        [ID] => 13
        [parentcat_ID] => 7
    ),
    Array
    (
        [ID] => 14
        [parentcat_ID] => 8
    )

)

但是我需要一个函数来递归地将每个项目放入相关父数组内的“子”数组中。所以它看起来更像这样:

Array
(
    Array
    (
        [ID] => 1
        [parentcat_ID] => 0
        [children] => Array (
            Array
            (
                [ID] => 6
                [parentcat_ID] => 1
                [childen] => Array (
                    Array
                    (
                        [ID] => 8
                        [parentcat_ID] => 6
                        [children] => Array (
                             Array
                             (
                                 [ID] => 14
                                 [parentcat_ID] => 8
                             )
                        )
                    )
                )
            ),
            Array
            (
                [ID] => 7
                [parentcat_ID] => 1
                [children] => Array(
                     Array
                     (
                         [ID] => 13
                         [parentcat_ID] => 7
                     )
                ) 
            ),
            Array
            (
                [ID] => 9
                [parentcat_ID] => 1
            )

        )
    )
    Array
    (
        [ID] => 2
        [parentcat_ID] => 0

    )

)

我希望这是有道理的!

4

2 回答 2

13

试一试(在 php 5.2 下测试):

$inArray = 数组(
    数组('ID' => '1', 'parentcat_ID' => '0'),
    数组('ID' => '2', 'parentcat_ID' => '0'),
    数组('ID' => '6', 'parentcat_ID' => '1'),  
    数组('ID' => '7', 'parentcat_ID' => '1'),
    数组('ID' => '8', 'parentcat_ID' => '6'),          
    数组('ID' => '9', 'parentcat_ID' => '1'),  
    数组('ID' => '13', 'parentcat_ID' => '7'),
    数组('ID' => '14','parentcat_ID' => '8'),     
);

函数 makeParentChildRelations(&$inArray, &$outArray, $currentParentId = 0) {
    if(!is_array($inArray)) {
        返回;
    }

    if(!is_array($outArray)) {
        返回;
    }

    foreach($inArray as $key => $tuple) {
        if($tuple['parentcat_ID'] == $currentParentId) {
            $tuple['children'] = array();
            makeParentChildRelations($inArray, $tuple['children'], $tuple['ID']);
            $outArray[] = $元组;   
        }
    }
}

$outArray = 数组();
makeParentChildRelations($inArray, $outArray);

print_r($outArray);
于 2010-01-19T16:33:04.010 回答
3

我最近回答了一个类似的问题。在这里。希望它适合您的需求。如果没有,请告诉我,我会根据您的规格进行调整。

编辑
好吧,这是调整后的版本,应该适合您的需要。

function generateMultiArray( array $flatArray )
{

    // initiate result array
    $multiArray = array();

    // iterate $flatArray
    foreach( $flatArray as $item )
    {
        // for convenience, initiate these vars
        $id = $item[ 'ID' ];
        $parentId = $item[ 'parentcat_ID' ];

        // initiate this item's children array;
        $item[ 'children' ] = array();

        // if parent doesn't exist yet, initiate it along with an empty 'children' array
        if( !isset( $multiArray[ $parentId ] ) )
        {
            $multiArray[ $parentId ] = array(
                'children' => array()
            );
        }

        // if this item is initiated already (as being a parent) merge it with the current item
        $multiArray[ $id ] = isset( $multiArray[ $id ] ) ? $multiArray[ $id ] + $item : $item;

        // add this item to the parents children collection by reference (for efficiency)
        $multiArray[ $parentId ][ 'children' ][ $id ] = &$multiArray[ $id ];

    }

    return $multiArray;
}

请注意,此函数还使所有项都可以作为结果数组的根项访问,并以它们的 ID 作为索引。

因此,要访问具有任意 id n 的项的子项,您将执行以下操作:

$multiArray = generateMultiArray( $yourFlatArray );
$children = $multiArray[ n ][ 'children' ]; // replace n with the id

编辑 2
忘记为非父项启动子项数组;现在添加。否则,在尝试通过以下方式访问它时会发出通知:

$multiArray = generateMultiArray( $yourFlatArray );
$children = $multiArray[ $someIdWithoutChildren ][ 'children' ];
于 2010-01-19T14:48:38.197 回答