【问题标题】:How do I add an additional child array recursively within last array in PHP如何在 PHP 的最后一个数组中递归地添加一个额外的子数组
【发布时间】:2012-08-24 22:42:27
【问题描述】:

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

这是我的数组的一个示例,

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 个子级缩进。它必须使用每个数组的数组中的最后一个数组。

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

感谢您的时间和精力。

【问题讨论】:

  • 你能修改你的问题吗?创建一个有效的 PHP 数组而不是 var_dump() 结果
  • @diEcho - 这是创建该数组的非常复杂的代码,我在寻找一个函数,该函数遍历我给它的数组并完成工作。我尝试使用 array_walk_recursive() 但我无法弄清楚。

标签: php arrays loops multidimensional-array foreach


【解决方案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);

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 2016-08-16
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    相关资源
    最近更新 更多