40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
![]() |
<?php
|
|||
|
/**
|
|||
|
* 格式化数据结构类
|
|||
|
*
|
|||
|
* @author:刘孝全
|
|||
|
* @email:q8197264@126.com
|
|||
|
* @date :2023年03月2日
|
|||
|
*/
|
|||
|
namespace app\common\controller;
|
|||
|
|
|||
|
class FormatList
|
|||
|
{
|
|||
|
/**
|
|||
|
* 无线级分类处理
|
|||
|
*
|
|||
|
* @param array $data 数据源
|
|||
|
* @param string $idName 主键
|
|||
|
* @param string $fieldName 父级字段
|
|||
|
* @param string $childrenKey 子级字段名
|
|||
|
* @return array
|
|||
|
*
|
|||
|
* @date 2020-03-27
|
|||
|
*/
|
|||
|
function FormatCategory(array $data, string $idName = "id", string $fieldName = 'pid', $childrenKey = 'children')
|
|||
|
{
|
|||
|
$items = [];
|
|||
|
foreach ($data as $item) {
|
|||
|
$items[$item[$idName]] = $item;
|
|||
|
}
|
|||
|
$result = array();
|
|||
|
foreach ($items as $item) {
|
|||
|
if (isset($items[$item[$fieldName]])) {
|
|||
|
$items[$item[$fieldName]][$childrenKey][] = &$items[$item[$idName]];
|
|||
|
} else if ($item[$fieldName] == 0) {
|
|||
|
$result[] = &$items[$item[$idName]];
|
|||
|
}
|
|||
|
}
|
|||
|
return $result;
|
|||
|
}
|
|||
|
}
|