早睡早起,方能养生!
Sleep early rise early, way to keep healthy!

PHP Mysql 无限级递归查询

super
2022-01-27 14:47
views 301

/**
 * 获取所有下级
 * @param int $superior_id 上级ID值
 * @return array
 */
public function getAllSub($superior_id)
{
	$array = [];
	$data = Db::name('tb_name')->where('superior_id', $superior_id)->field('id')->select();
	if ($data) {
		$ids = array_column($data, 'id');
		$array = array_merge($array, $ids);
		foreach ($data as $k => $v) {
			$array = array_merge($array, $this->getAllSub($v['id']));
		}
	}
	return $array;
}

 

superior_id是数据库中上级ID字段

 

如果想变成树形结构,请看这篇文章:PHP二维数组转树形,树形转二维数组

 

禁止转载



分享
0 条讨论
top