php 无限极分类 递归 封转的类库,到手即用

19 篇文章 0 订阅
2 篇文章 0 订阅
本文介绍了如何使用PHP中的AppFunc类,包括arrayToTree方法将二维数组转换为树形结构,以及getRandomStr函数生成随机字符串。重点展示了如何展示列表和递归遍历树结构,适合快速理解并应用于项目中。
摘要由CSDN通过智能技术生成

核心代码


<?php

namespace App\Common;


use easySwoole\Cache\Cache;

class AppFunc
{
    // 二维数组 转 tree
    public static function arrayToTree($list, $pid = 'pid')
    {
        $map = [];
        if (is_array($list)) {
            foreach ($list as $k => $v) {
                $map[$v[$pid]][] = $v; // 同一个pid 放在同一个数组中
            }
        }

        return self::makeTree($map);
    }

    private static function makeTree($list, $parent_id = 0)
    {
        $items = isset($list[$parent_id]) ? $list[$parent_id] : [];
        if (!$items) {
            return null;
        }

        $trees = [];
        foreach ($items as $k => $v) {
            $children = self::makeTree($list, $v['id']); // 找到以这个id 为pid 的数据
            if ($children) {
                $v['children'] = $children;
            }
            $trees[] = $v;
        }

        return $trees;
    }

    /**
     * / 规则 |--- 就分的
     * @param  [type] $tree_list [树 数组]
     * @param  [type] &$tree     [返回的二维数组]
     * @param  [type] $name      [那个字段进行 拼接|-- ]
     * @param  string $pre       [前缀]
     * @param  string $child     [树 的子分支]
     * @return [type]            [description]
     */
    public static function treeRule($tree_list, &$tree, $pre = '', $name = 'name', $child = 'children')
    {
        if (is_array($tree_list)) {
            foreach ($tree_list as $k => $v) {
                $v[$name] = $pre . $v[$name];
                $tree[]    = $v;
                if (isset($v[$child])) {
                    self::treeRule($v[$child], $tree, $pre . '&nbsp;|------&nbsp;');
                }
            }
        }
    }

    /**
     * 获得随机字符串
     * @param $len             需要的长度
     * @param $special        是否需要特殊符号
     * @return string       返回随机字符串
     */
    public static function getRandomStr($len, $special = true)
    {
        $chars = [
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
            "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
            "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
            "3", "4", "5", "6", "7", "8", "9"
        ];

        if ($special) {
            $chars = array_merge($chars, [
                "!", "@", "#", "$", "?", "|", "{", "/", ":", ";",
                "%", "^", "&", "*", "(", ")", "-", "_", "[", "]",
                "}", "<", ">", "~", "+", "=", ",", "."
            ]);
        }

        $charsLen = count($chars) - 1;
        shuffle($chars); //打乱数组顺序
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $str .= $chars[mt_rand(0, $charsLen)]; //随机取出一位
        }
        return $str;
    }
}

如何使用

列表显示

在这里插入图片描述

在这里插入图片描述

children 显示

在这里插入图片描述

在这里插入图片描述

如果不行的话 那我也不知道了,不是我写了。
无意中看到,然后就记录了一下,比较 这个是常用的,下次找也比较方便

来源地址:
https://github.com/xiaobaiskill/easyswoole-admin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值