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 = 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')、     
);

関数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 [] = $ tuple;   
        }
    }
}

$ outArray = array();
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 に答える