层级展示和父子级结构的封装推荐~🎁
在 laravel 中 封装 common 文件:在app下创建一个以 .php 结尾的文件
在 common 文件中做分装方法 :function_exists()
层级展示:
if(!function_exists("treelevel")){
function treelevel(array $data, int $pid =0 , string $html = "=",int $level = 0){
static $arr = [];
foreach ($data as $val){
if($pid ==$val['pid']){
$val['html'] = str_repeat($html,$level * 2);
$val['level'] = $level + 1;
$arr[] = $val;
treelevel($data ,$val['id'],$html,$val['level']);
}} return $arr;
}}
树状数组处理
if(!function_exists('tree_list')){
//引用方式实现 父子级树状结构
function tree_list($list){
//将每条数据中作为其下标的id值
$temp = [];
foreach($list as $v){
$v['son'] = [];
$temp[$v['id']] = $v;
} //获取分类树
foreach($temp as $k=>$v){
$temp[$v['pid']]['son'][] = &$temp[$v['id']];
} return isset($temp[0]['son']) ? $temp[0]['son'] : [];
}}
封装完之后 在终端执行composer 命令
composer dump-autoload
本作品采用《CC 协议》,转载必须注明作者和本文链接