常用代码块(基于ThinkPHP 框架)

目录

递归类

请求类

常用函数


递归类

  1. 无限上级
      function shangji($uid, &$array = [])
        {
            $is_parent = Db::name('user')->where('user_id', $uid)->field('father_id')->find();
    
            if ($is_parent["father_id"] != 0) {
                $array[] = $is_parent['father_id'];
                $this->shangji($is_parent['father_id'], $array);
            }
            return $array;
        }

  2. 无限下级
    function xiaji($user_id, &$result = [])
        {
            $list = Db::name('user')->where('father_user_id', $user_id)->column('id');
    
            if (!empty($list)) {
                foreach ($list as $key => $val) {
    
                    $result[] = $val;
                    $this->xiaji($val, $result);
                }
            }
            return $result;
        }

请求类

  1. 解决thinkphp6跨域问题:(转载至)https://www.csdn.net/tags/MtTaEgysMDYxODcyLWJsb2cO0O0O.htmlicon-default.png?t=M3C8https://www.csdn.net/tags/MtTaEgysMDYxODcyLWJsb2cO0O0O.html
  2. 模拟GET请求
    function curl_get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
        curl_setopt($ch, CURLOPT_HEADER, 0);
    
        $output = curl_exec($ch);
        curl_close($ch);
    
        return $output;
    }
  3. 模拟POST请求
    function curl_post($url, $array)
    {
    
        $curl = curl_init();
    
        curl_setopt($curl, CURLOPT_URL, $url);
     
        curl_setopt($curl, CURLOPT_HEADER, 0);
      
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     
        curl_setopt($curl, CURLOPT_POST, 1);
    
        $post_data = $array;
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
       
        $data = curl_exec($curl);
    
        curl_close($curl);
    
        return $data;
    }

常用函数

function daily_list()
    {
        round('XXX', 2);//保留N位小数

        json_encode($array, JSON_UNESCAPED_UNICODE);//数组转JSON,保留中文

        json_decode($JSON, false);//JSON转数组,TRUE为对象

        substr("Hello world", 6);//从第六位开始截取字符串,负数为从后面截取

        implode(',', $array);//使用逗号分隔数组为字符串

        explode(',', $string);//根据逗号将字符串转为数组

        array_unique($array);//数组去重

        asort($array); //根据关联数组的值,对数组进行升序排列

        ksort($array);// 根据关联数组的键,对数组进行升序排列

        arsort($array);// 根据关联数组的值,对数组进行降序排列

        krsort($array);// 根据关联数组的键,对数组进行降序排列
    }

后期不断完善

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值