0

I am working on tree formatting of an 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 the basis of ['member'] key, I'm trying to display the 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 like below to get the incremented row count for every array element and column count to 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 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;
            }

        }
    }
5
  • 1
    And the million dollar question is? Commented Jun 4, 2015 at 7:22
  • @Epodax: On the basis of ['member'] key, trying to display tree structure in parent-child formation like I have displayed in question. but getting some unordered sequence. can you help me in this recursive function? Commented Jun 4, 2015 at 7:27
  • @Epodax: And what you cant of this? Commented Jun 4, 2015 at 8:52
  • @Epodax it turns out like this :) Commented Jun 4, 2015 at 8:56
  • I was mistaken in adress. Wanted to write to asker :) Commented Jun 4, 2015 at 9:02

1 Answer 1

1

Make it easy

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, '');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.