Link to home
Start Free TrialLog in
Avatar of mrh14852
mrh14852

asked on

Need an array conversion function.

My database outputs a flat array that looks like this.

$arr = array(
            array(
                'director' => 'ABC',
                'manager' => '123',
                'sub' => 'QQQ'
            ),
            array(
                'director' => 'ABC',
                'manager' => '123',
                'sub' => 'WWW'
            ),
            array(
                'director' => 'ABC',
                'manager' => 'ABC',
                'sub' => 'EEE'
            ),
            array(
                'director' => 'ABC',
                'manager' => 'ABC',
                'sub' => 'RRR'
            ),
            array(
                'director' => 'DEF',
                'manager' => '456',
                'sub' => 'TTT'
            ),
            array(
                'director' => 'DEF',
                'manager' => '456',
                'sub' => 'YYY'
            ),
            array(
                'director' => 'DEF',
                'manager' => 'DEF',
                'sub' => 'UUU'
            ),
            array(
                'director' => 'DEF',
                'manager' => 'DEF',
                'sub' => 'III'
            )
        );

Open in new window


I need to get it into an array that looks like this.

$arr2 = array(
            array(
                'text' => 'ABC',
                'id' => 'ABC',
                'expanded' => FALSE,
                'leaf' => FALSE,
                'children' => array(
                    array(
                        'text' => '123',
                        'expanded' => FALSE,
                        'leaf' => FALSE,
                        'children' => array(
                            array(
                                'text' => 'QQQ',
                                'leaf' => TRUE
                            ),
                            array(
                                'text' => 'WWW',
                                'leaf' => TRUE
                            )
                        )
                    ),
                    array(
                        'parentId' => 'ABC',
                        'text' => 'EEE',
                        'leaf' => TRUE
                    ),
                    array(
                        'parentId' => 'ABC',
                        'text' => 'RRR',
                        'leaf' => TRUE
                    )
                )
            ),
            array(
                'text' => 'DEF',
                'expanded' => FALSE,
                'leaf' => FALSE,
                'children' => array(
                    array(
                        'text' => '456',
                        'expanded' => FALSE,
                        'leaf' => FALSE,
                        'children' => array(
                            array(
                                'text' => 'TTT',
                                'leaf' => TRUE
                            ),
                            array(
                                'text' => 'YYY',
                                'leaf' => TRUE
                            )
                        )
                    ),
                    array(
                        'text' => 'UUU',
                        'leaf' => TRUE
                    ),
                    array(
                        'text' => 'III',
                        'leaf' => TRUE
                    )
                )
            )
        );

Open in new window


If you look at the second array.  "sub" will always be a leaf.  Sometimes the "director" and "manager" will be the same person.

Thanks for your help.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Why do some nodes have parentid and others not?
ASKER CERTIFIED SOLUTION
Avatar of tg_wilk
tg_wilk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrh14852
mrh14852

ASKER

Finally had time to try this.  Had to modify a bit but this got me in the right direction.  Really appreciate you taking the time.