标签:new code HERE 数组 tree key model amp class
public function getPrivilegeList($moduleType){ $authRuleModel = M(‘auth_rule‘); $data = $authRuleModel->where([‘module_type‘ => $moduleType])->select(); $res = $this->getChild($data); return $res; } public function getChild($data) { $tree = []; $newData = []; //循环重新排列 foreach ($data as $datum) { $newData[$datum[‘id‘]] = $datum; } foreach ($newData as $key => $datum) { if ($datum[‘pid‘] > 0) { //不是根节点的将自己的地址放到父级的child节点 $newData[$datum[‘pid‘]][‘child‘][] = &$newData[$key]; } else { //根节点直接把地址放到新数组中 $tree[] = &$newData[$datum[‘id‘]]; } } return $tree; }
标签:new code HERE 数组 tree key model amp class
原文地址:https://www.cnblogs.com/gwhm/p/13721254.html