/**
* 数据树形结构化
* @Author: Faulk.yao
* @Time: 2020/3/3 14:07
* @param array $list 需树形结构化数据数组列表
* @param int $topValue 最顶级上级ID值
* @param string $topField 顶级栏目字段名
* @param string $contact 子类关联字段名
* @param string $child 结构化子栏目字段名
* @return array
*/
public static function getTree($list, $topValue = 0, $topField = 'id', $contact = 'pid', $childKey = 'child')
{
$child = [];
if (!empty($list)) {
foreach ($list as $key => $value) {
if ($value[$contact] == $topValue) {
$child[$value[$topField]] = $value;
unset($list[$key]);
$data = self::getTree(
$list,
$value[$topField],
$topField,
$contact,
$childKey
);
if (!empty($data)) {
$child[$value[$topField]][$childKey] = $data;
}
}
}
}
return array_values($child);
}
分类等可用
最新推荐文章于 2024-04-23 09:43:14 发布