php - add to deepest array child element -
how select deepest child element multidimensional-array in order add element specific 1 in php (always knowing added, in case additional information parent needed in order create new child)?
eg. having array want able add aaaa, bbbb, cccc each: aaa, bbb, ccc? , later on aaaaa, bbbbb,… each aaaa, bbbb… , on (where each new element 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" ), […] ), );
you might want use array_walk_recursive
. lets itterate on leafs of multidimensional array.
function add_children(&$item, $key) { //here can change item else (since use &) $item = array('aaaa', 'bbbb', 'cccc'); } array_walk_recursive($array, 'add_children');
disclaimar: have not tested code.
Comments
Post a Comment