2

我正在尝试将另一个子数组添加到最后一个子数组。

这是我的数组的示例,

Array
(
    [Auto-Trail] => Array
        (
            [name] => Auto-Trail
            [children] => Array
                (
                    [2001] => Array
                        (
                            [name] => 2001
                            [children] => Array
                                (
                                    [Tracker] => Array
                                        (
                                            [name] => Tracker
                                            [children] => Array
                                                (
                                                    [CK] => Array
                                                        (
                                                            [name] => CK
                                                            [children] => Array
                                                                (
                                                                    [Fiat] => Array
                                                                        (
                                                                            [name] => Fiat
                                                                        )

                                                                )

                                                        )

                                                    [EK] => Array
                                                        (
                                                            [name] => EK
                                                            [children] => Array
                                                                (
                                                                    [Fiat] => Array
                                                                        (
                                                                            [name] => Fiat
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                    [Cheyenne] => Array
                                        (
                                            [name] => Cheyenne
                                            [children] => Array
                                                (
                                                    [630S] => Array
                                                        (
                                                            [name] => 630S
                                                            [children] => Array
                                                                (
                                                                    [Fiat] => Array
                                                                        (
                                                                            [name] => Fiat
                                                                        )

                                                                    [Merc] => Array
                                                                        (
                                                                            [name] => Merc
                                                                            [children] => Array
                                                                                (
                                                                                    [313] => Array
                                                                                        (
                                                                                            [name] => 313
                                                                                        )

                                                                                    [316] => Array
                                                                                        (
                                                                                            [name] => 316
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                    [630] => Array
                                                        (
                                                            [name] => 630
                                                            [children] => Array
                                                                (
                                                                    [Fiat] => Array
                                                                        (
                                                                            [name] => Fiat
                                                                        )

                                                                    [Merc] => Array
                                                                        (
                                                                            [name] => Merc
                                                                            [children] => Array
                                                                                (
                                                                                    [313] => Array
                                                                                        (
                                                                                            [name] => 313
                                                                                        )

                                                                                    [316] => Array
                                                                                        (
                                                                                            [name] => 316
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                    [634] => Array
                                                        (
                                                            [name] => 634
                                                            [children] => Array
                                                                (
                                                                    [Fiat] => Array
                                                                        (
                                                                            [name] => Fiat
                                                                        )

                                                                    [Merc] => Array
                                                                        (
                                                                            [name] => Merc
                                                                            [children] => Array
                                                                                (
                                                                                    [313] => Array
                                                                                        (
                                                                                            [name] => 313
                                                                                        )

                                                                                    [316] => Array
                                                                                        (
                                                                                            [name] => 316
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                    [634U] => Array
                                                        (
                                                            [name] => 634U
                                                            [children] => Array
                                                                (
                                                                    [Fiat] => Array
                                                                        (
                                                                            [name] => Fiat
                                                                        )

                                                                    [Merc] => Array
                                                                        (
                                                                            [name] => Merc
                                                                            [children] => Array
                                                                                (
                                                                                    [313] => Array
                                                                                        (
                                                                                            [name] => 313
                                                                                        )

                                                                                    [316] => Array
                                                                                        (
                                                                                            [name] => 316
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

这是我希望添加到每个最后一个元素的数组,

例如,我需要将以下内容添加到 [CK][children][Fiat][children] (以及每个最后一个孩子的孩子) - 这个数组将是静态的,每个最后一个孩子都将使用相同的数组。

[BOF] => Array
    (
        [name] => Furniture
    )

[CHA] => Array
    (
        [name] => Chassis
    )

所以... ][CK][children][Fiat] 会变成下面这样,

                                            [CK] => Array
                                                (
                                                    [name] => CK
                                                    [children] => Array
                                                        (
                                                            [Fiat] => Array
                                                                (
                                                                    [name] => Fiat
                                                                        [children] => Array
                                                                            (
                                                                                [BOF] => Array
                                                                                    (
                                                                                        [name] => Furniture
                                                                                    )

                                                                                [CHA] => Array
                                                                                    (
                                                                                        [name] => Chassis
                                                                                    )
                                                                            )
                                                                )

                                                        )

                                                )

请注意,并非每个最后一个孩子的缩进级别都相同,可能还有 10 个孩子缩进。它必须使用每个数组的数组中的最后一个数组。

抱歉,如果很难理解,我正在努力准确地说出我需要它的工作方式。

感谢您的时间和精力。

4

1 回答 1

1

看看这个解决方案(quick'n'dirty):

function injectArray(array $target, array $inject, $depth = 0){
    $hasChildrenKey = array_key_exists('children', $target);
    $hasChildren = $hasChildrenKey && !empty($target['children']);

    if($depth % 2 == 0){
        foreach($target as $k => $v)
            if(is_array($v))
                $target[$k] = injectArray($v, $inject, $depth+1);
    }
    elseif(!$hasChildren){
        if(!$hasChildrenKey)    
            $target['children'] = array();
        $target['children'] = array_merge_recursive($target['children'], $inject);
    }
    else if($hasChildrenKey){
        $target['children'] = injectArray($target['children'], $inject, $depth+1);
    }
    return $target;
};

$testArray = array(
    'Auto-Trail' => array('name' => 'asdf', 
        'children' => array(
            '2001' => array('name' => '2001',
                'children' => array(
                    'Tracker' => array('name' => 'Tracker',
                        'children' =>   array(
                            'CK' => array('name' => 'CK',
                                'children' => array(
                                    'Fiat' => array('name' => 'Fiat')
                                )
                            )
                        )                               
                    )
                )
            )
        )
    )
);

$testInjectArray = array('BOF' => array('name' => 'Furniture'), 'CHA' => array('name' => 'Chassis'));

$result = injectArray($testArray, $testInjectArray);
print_r($result);
于 2012-08-30T10:30:24.097 回答