标签:key color 默认 while res name flag array 创建
/** * 批量更新xw_kefu的sonIds * @author jack */ function kefu_update_kefuids() { set_time_limit(0); global $db; $sql = "select id,name,p_id,is_lizhi from xw_kefu where is_del=0 and is_lizhi=0 and kefu_type=‘o2o‘"; $result = $db->query($sql); $arrays = $db->fetch_array_all($result); $tmpArr = $arrays; foreach( $arrays as $key=>$row ){ $kefuid = $row[‘id‘]; $son_ids = subtree($tmpArr, $kefuid); $son_ids = implode(‘,‘, $son_ids); $sql2 = "update xw_kefu set son_ids=‘$son_ids‘ where id=$kefuid"; $db->query($sql2); } } /** * 获取下级顾问所有ID集合 * @author jack */ function subtree($arr, $pId=0) { //创建任务表 $task = [$pId]; // 保存子ids的数组 $ids = []; //如果任务表不为空 就表示要做任务 while(!empty($task)) { //默认是没有子级 $flag = false; foreach($arr as $k=>$v) { if ($v[‘p_id‘] == $pId) { $ids[] = $v[‘id‘]; //借助栈 把新的地区的id压入栈 $task[] = $v[‘id‘]; $pId = $v[‘id‘]; //把找到的单元unset掉 unset($arr[$k]); $flag = true; } } //表示没找到子树 if (!$flag) { array_pop($task); $pId = end($task); } } return $ids; }
标签:key color 默认 while res name flag array 创建
原文地址:https://www.cnblogs.com/dawuge/p/13185034.html