Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8490869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:15:04+00:00 2026-06-10T22:15:04+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:15:06+00:00Added an answer on June 10, 2026 at 10:15 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to open an existing pdf file and then add another page to
I am attempting add a tableadapter to a stored procedure in my SQL Server
I am attempting add some tests to an existing QT GUI application using QTest.
I am attempting to add a function that will create a csv for download
I'm attempting to add a legend to a graph, and I want to append
I'm attempting to add a bookmarklet to my wop website. The issue is that
I'm attempting to add a cmake build system to an old fortran code that
I am attempting to add an update method to the Symbol class. class SymbolUpdate(s:
I am attempting to add a simple toolstrip however I can't get it to
I'm attempting to add a group of controls to a page when a button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.