如何构建遍历树?

时间:2015-06-04 07:21:38

标签: php arrays function recursion tree

我正在研究关联数组的树格式:

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
                                                (
                                                )

                                        )
                                 )
                        )
                )
        )
)

['member']键的基础上,我试图以父子形式显示树结构,如:

David Clance
    Sonali Yadav
        Samina Falgun
            Rushi Vyas
                Vijay Gade
            Pranali Dighe
        Akash Meheta
            Rajendra Gore
    Dharmendra Shroff
        James Bond

我有类似下面的代码来获取每个数组元素的递增行数和列数到列中的选项卡值:

    $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 as $keys => $values ) {

            $this->m_intRowCount++; 
            echo $values['name'] . " " . $values['designation'];

            if( true == valArr( $values['member'] ) ) {
                echo "<br>";
                $intColumnCount++;

                foreach( $values['member'] as $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;
            }

        }
    }

1 个答案:

答案 0 :(得分:1)

轻松实现

function out($in, $space) {
  foreach($in as $arr) {
    echo $space.$arr['name']."\n";
    if (is_array($arr['member']) && count($arr['member'])) out($arr['member'], $space."   ");
  }  
}

out($array, '');