PHP查找所有下级用户
/1.用户数据结构 $users = array( array('id'=>1, 'pid'=>0), array('id'=>2, 'pid'=>1), array('id'=>3, 'pid'=>1), array('id'=>4, 'pid'=>8), array('id'=>5, 'pid'=>3), array('id'=>6, 'pid'=>3), array('id'=>7, 'pid'=>3), array('id'=>8, 'pid'=>8) );
2. 获取团队所有下级用户 /** * 获取团队所有下级用户 * @param $users * @param $mid * @return array[id, pid] */ public static function getTeamUser($mid, $users) { $teams = array(); $mids = array($mid); do { $othermids = array(); $state = false; foreach ($mids as $valueone) { foreach ($users as $key => $valuetwo) { if ($valuetwo['pid'] == $valueone) { $teams[] = $valuetwo['id']; //找到我的下级立即添加到最终结果中 $othermids[] = $valuetwo['id']; //将我的下级id保存起来用来下轮循环他的下级 array_splice($users, $key, 1); //删除这个用户 $state = true; } } } $mids = $othermids; //foreach中找到的我的下级集合,用来下次循环 } while ($state == true); return $teams; }
3. 一维数组转为字符串 /** * 一维数组转为字符串 * @param $arr * @return string */ public static function convertArrayToString($arr) { if (!empty($arr)) { $strings = ''; foreach ($arr as $item) { $s = (string)$item; $strings .= $s . ','; } $string = rtrim($strings, ','); //删除数组最后一个字符 return $string; } return null; }
4. if ($usersSubId) {array_push($team, $usersSubId);};