Build binary tree use references of object by php

CAKE PHP

Posted on 2015-08-05 15:52:44


 

<?php
 $users = array ( array( 'id' => 1, 'manager_id' => 2, 'name' => 'A' ), array( 'id' => 2, 'manager_id' => 4, 'name' => 'B' ), array( 'id' => 3, 'manager_id' => 2, 'name' => 'C' ), array( 'id' => 4, 'manager_id' => 100, 'name' => 'D' ), array( 'id' => 5, 'manager_id' => 4, 'name' => 'E' ), array( 'id' => 6, 'manager_id' => 100, 'name' => 'F' ), array( 'id' => 7, 'manager_id' => 6, 'name' => 'G' ), );
$managerIds = array (2,4,6); $objectUsers = array();
 foreach ($users as $user) 
$objectUsers[] = (object)$user;
 } 
$bosses = findBosses($objectUsers, $managerIds); 
buildTree($objectUsers); 
//print_r($bosses); die; 
 echo "<pre>"; 
print_r($bosses);die; 
function buildTree($users) {
foreach ($users as $user) { 
foreach ($users as $manager) { 
if ($user->manager_id == $manager->id) { 
isset ($manager->staff) || $manager->staff = array(); $manager- >staff[] = $user; 
}
 }
function findBosses($users, $managerIds) { 
$bosses = array(); foreach ($users as $user) { 
if (!in_array($user->manager_id, $managerIds)) { 
$bosses[] = $user; 
return $bosses; 
}  

1. Function findBosses will be found parent note for tree. 2. Function buildTree implement set child note to parent note on binary tree. You could copy behind content php and run demo. You will see result. Here is result on my local

Array
(
    [0] => stdClass Object
        (
            [id] => 4
            [manager_id] => 100
            [name] => D
            [staff] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 2
                            [manager_id] => 4
                            [name] => B
                            [staff] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 1
                                            [manager_id] => 2
                                            [name] => A
                                        )

                                    [1] => stdClass Object
                                        (
                                            [id] => 3
                                            [manager_id] => 2
                                            [name] => C
                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 5
                            [manager_id] => 4
                            [name] => E
                        )

                )

        )

    [1] => stdClass Object
        (
            [id] => 6
            [manager_id] => 100
            [name] => F
            [staff] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 7
                            [manager_id] => 6
                            [name] => G
                        )

                )

        )

)