<?php
namespace app\common\utils;
//一个生成树的类库
class TreeUtils
{
public static function sort($dataArr, $rootId = 0, $pkName = 'id', $pIdName = 'pid', $childName = 'children')
{
$arr = [];
foreach ($dataArr as $sorData) {
if ($sorData[$pIdName] == $rootId) {
$children = TreeUtils::sort($dataArr, $sorData[$pkName]);
if($children){
$sorData[$childName] = $children;
}
$arr[] = $sorData;
}
}
return $arr;
}
}
$dataArr:数据列表 $rootId:顶级ID值 $pkName:主键ID $pIdName:PID对应字段 $childName:生成树形后的对应子项字段名
评论