php - Linear representation of an array tree -
i have 1 1 linear tree languages => types => products => etc; languages having many types , types having many products , on.
i have written recursive function return array in following style:
array ( [0] => array ( [id] => 166 [name] => product1 [type] => product [depth] => 2 [parent] => array ( [0] => array ( [id] => 165 [name] => default [type] => type [depth] => 1 [parent] => array ( [0] => array ( [id] => 1 [name] => en [type] => language [depth] => 0 [parent] => false ) ) ) ) ) )
what want recursive method traverse tree , provide array such as
[0] => array( 'id' => 1, 'name' => 'en'), [1] => array( 'id' => 165, 'name' => 'default'), [2] => array( 'id' => 166, 'name' => 'product1')
with 0,1,2 being equal elements depth
can build breadcrumbs of data.
thank you.
the key here make print function you'll able call recursively. i'd suggest this
function print_recursive($array, $depth = 0) { //code print stuff //calls print function on parent if it's array if(is_array($array['parent'])) { print_recursive($array['parent'], $depth+1); } }
the depth parameter 0 default increment 1 when calling print_recursive on $array['parent']. way, each time deeper in array, it'll incremented.
Comments
Post a Comment