arrays - php recursion level -


i have recursion function. there hierarchy users structure. send user id function , should find user under this. function returns array of associates users. task find levels of users.

for example:

        user1        /    \     user2   user3    /    \    \  user4 user5  user6 

user1 have level 0. user2, user3 have level 1. user4, user5, user6 have level 2. how can find in recursion? code:

private function getassociates($userid) {     global $generation;     global $usersunder;     if (!isset($generation)) {         $generation = 1;     }     $userdb           =  new lyf_db_table('user');     $associatesselect =  $userdb->select();     $associatesselect -> from('user', array('id'))->where('enroller_id = ?', $userid);     $associates       =  $userdb->fetchall($associatesselect)->toarray();     if (!empty($associates)) {         foreach ($associates $associate) {             $usersunder[$generation] = $associate['id'];             $this->getassociates($associate['id']);         }     }     return $usersunder; } 

add parameter getassociates() function:

private function getassociates($userid, $level = 0) { 

and when you're processing level of tree, store $level rest of user data, recurse function with:

$this->getassociates($associate['id'], $level + 1); 

and when call function start process, pass in 0 $level, or leave blank , let php assign default (also 0).


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -