2

I am attempting to add another child array to the last child array.

Here is a sample of my array,

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
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

This is the array I wish to add to every last element,

For example, I need to add the following to [CK][children][Fiat][children] (and every last children's children) - this array will be static, every last child will use the same array.

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

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

So ... ][CK][children][Fiat] would become the following,

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

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

                                                        )

                                                )

Please note, Not every last child is the same indentation level, it could be 10 more children indentations. It must use the last array within the array of each array.

Apologies if it's hard to understand, I'm struggling to word exactly the way I need it to work.

Thank you for your time and effort.

2
  • 1
    can u modify your question? create a valid PHP array not the var_dump() result Commented Aug 30, 2012 at 8:34
  • @diEcho - it's very complex code which creates that array, I was sort of looking for a function of which walks through the array I give to it and does the job. I attempted to use array_walk_recursive() but I couldn't figure it out. Commented Aug 30, 2012 at 8:38

1 Answer 1

1

check out this solution (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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.