php - How to build traversing tree? -
i working on tree formatting of associative array:
array ( [8] => array ( [name] => david clance [designation] => chief executive officer [member] => array ( [62] => array ( [name] => sonali yadav [designation] => managing director [member] => array ( [1147] => array ( [name] => samina falgun [designation] => associate technical product manager [member] => array ( [676] => array ( [name] => rushi vyas [designation] => team lead [member] => array ( [946] => array ( [name] => vijay gade [designation] => software engineer [member] => array ( ) ) ) ) [1497] => array ( [name] => pranali dighe [designation] => software engineer [member] => array ( ) ) ) ) [2882] => array ( [name] => akash meheta [designation] => manager - administrations [member] => array ( [972] => array ( [name] => rajendra gore [designation] => office assistant [member] => array ( ) ) ) ) ) ) [189] => array ( [name] => dharmendra shroff [designation] => director of engineering [member] => array ( [443] => array ( [name] => james bond [designation] => software development manager [member] => array ( ) ) ) ) ) ) )
on basis of ['member']
key, i'm trying display tree structure in parent-child formation like:
david clance sonali yadav samina falgun rushi vyas vijay gade pranali dighe akash meheta rajendra gore dharmendra shroff james bond
i have code below incremented row count every array element , column count tab values in array:
$this->m_introwcount = 1; $intcolumncount = 0; public function handle() { if( true == valarr( $this->m_arrmixemployees ) ) { $this->getemployeedetails( $this->m_arrmixemployees, $intcolumncount ); } } public function getemployeedetails( $arrmixemployees, $intcolumncount ) { foreach( $arrmixemployees $keys => $values ) { $this->m_introwcount++; echo $values['name'] . " " . $values['designation']; if( true == valarr( $values['member'] ) ) { echo "<br>"; $intcolumncount++; foreach( $values['member'] $key => $value ) { $this->m_introwcount++; echo $value['name'] . " " . $value['designation']; if( true == valarr( $value['member'] ) ) { echo "<br>"; $intcolumncount++; $this->getemployeedetails( $value['member'], $intcolumncount ); $intcolumncount--; } else { echo "<br>"; } } } else { echo "<br>"; continue; } } }
make easy
function out($in, $space) { foreach($in $arr) { echo $space.$arr['name']."\n"; if (is_array($arr['member']) && count($arr['member'])) out($arr['member'], $space." "); } } out($array, '');
Comments
Post a Comment