add to deepest array child element

cocoseis

How do I select the deepest child element from a multidimensional-array in order to add another element to that specific one in php (always knowing where it is added, so in case additional information from the parent is needed in order to create the new child)?

eg. having an array like this I want to be able to add another aaaa, bbbb, cccc to each: aaa, bbb, ccc? and later on aaaaa, bbbbb,… to each aaaa, bbbb… and so on (where each new element is aware of it's parent's name).

array(
"a"=> array(
    "aa"=> array(
       "aaa",
       "bbb",
       "ccc"
       ),
    "bb"=> array(
       "aaa",
       "bbb",
       "ccc"
       ),
    "aa"=> array(
       "aaa",
       "bbb",
       "ccc"
       ),
    ),
"b"=> array(
    "aa"=> array(
       "aaa",
       "bbb",
       "ccc"
       ),
    "bb"=> array(
       "aaa",
       "bbb",
       "ccc"
       ),
    "aa"=> array(
       "aaa",
       "bbb",
       "ccc"
       ),
    ),
"c"=> array(
    "aa"=> array(
       "aaa",
       "bbb",
       "ccc"
       ),
       […]
    ),
);
Anders

You might want to use array_walk_recursive. It lets you itterate over all the leafs of a multidimensional array.

function add_children(&$item, $key)
{
    //Here you can change item to something else (since we use &)
    $item = array('aaaa', 'bbbb', 'cccc');
}

array_walk_recursive($array, 'add_children');

Disclaimar: I have not tested this code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Add an element to an array in Swift

From Dev

How to add a child element for XML in PowerShell

From Dev

Target the deepest nested ul/ol element with css

From Dev

add/insert child element(s) using XSLT

From Dev

Using jQuery is there a way to find the farthest (deepest, or most nested) child element?

From Dev

Add element to array with Reference

From Dev

Find deepest child, :contains(string)

From Dev

Create div element and dynamically add child elements on it

From Dev

how to add element to array

From Dev

Add element to empty array

From Dev

Add class to child element but not to parent

From Dev

how to add child element in navigationdrawer with expandble listview

From Dev

Add element into Array

From Dev

Add or remove element in array

From Dev

Assign 'active' class only to deepest child

From Dev

Add or update an array element

From Dev

How to add child element after Root element

From Dev

add element to array in PHP

From Dev

how to add element to array

From Dev

Select image child element in PHP array, get source and add the source in a link

From Dev

Add child element based on directive and recompile?

From Dev

Add style to every element child using loops

From Dev

jQuery get deepest inner element

From Dev

Add class if child element not present

From Dev

Add a subdocument in array element

From Dev

Add Child Element To XML Element using Powershell

From Dev

Mongodb: add field to matching child element

From Dev

Add element to Firestore array

From Dev

dynamically add attribute to child element