zoukankan      html  css  js  c++  java
  • thinkphp中定义自己的函数

    可以在前台和后台的公共文件夹中common.php中定义自己的函数,这样就可以在控制器中调用,而不需要调用对象了

    /**
    * @name addvtorandp
    * @author 黄峰1664253936@qq.com
    * @version 在索引数组任意位置插入元素
    */
    public function addvtorandp($array,$position,$value){
    $tmp=array();
    for($i=0;$i<=count($array);$i++){
    if($i==$position){
    $tmp[$position]=$value;
    }elseif($i<$position){
    $tmp[$i]=$array[$i];
    }else{
    $tmp[$i]=$array[$i-1];
    }
    }
    return $tmp;
    }


    /**
    * @name getCompleteArr
    * @author 黄峰1664253936@qq.com
    * @param beginTime开始时间,endTime结束时间,$list非完整数据二位数组
    * @return countArr统计数的索引数组,dateArr日期的索引数组
    * @version 如果日期没有设置那么begin日期就是15days ago,endTime=nowdate
    */
    public function getCompleteArr($endtime,$misdays=15,$list)
    {
    foreach ($list as $vo) {
    $dateArr[]=$vo[day];
    $countArr[]=$vo[count];
    }
    $maxDate=$endtime;
    $maxDay=intval(substr($maxDate, -2));
    $maxMon=intval(substr($maxDate,strlen($maxDate)-4,2));
    //下面是回溯到上一月的情况
    if($maxDay<=$misdays)
    {
    $minMon=$maxMon-1;
    $minMon=sprintf("%02d",$minMon);
    //上月是大月
    if($minMon==1||$minMon==3||$minMon==5||$minMon==7||$minMon==8||$minMon==10||$minMon==12){
    $minDay=31-($misdays-$maxDay);
    $j=0;
    for($i=$minDay;$i<=31;$i++){
    if($i<10){
    $i="0".$i;
    }
    $tmpDate=substr($maxDate,0,4)."$minMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){

    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    $maxMon = sprintf("%02d",$maxMon);
    for($i=1;$i<$maxDay;$i++){
    $i=sprintf("%02d",$i);
    $tmpDate=substr($maxDate,0,4)."$maxMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){
    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    }else{
    //如果上月是小月
    $minDay=30-($misdays-$maxDay);
    $j=0;
    for($i=$minDay;$i<=30;$i++){
    $i=sprintf("%02d",$i);
    $minMon = sprintf("%02d",$minMon);
    $tmpDate=substr($maxDate,0,4)."$minMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){

    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    $maxMon=sprintf("%02d",$maxMon);
    for($i=1;$i<$maxDay;$i++){
    $i=sprintf("%02d",$i);
    $tmpDate=substr($maxDate,0,4)."$maxMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){
    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    }
    //endTag上面是需要回溯到上一月的情况
    }else{
    //下面是不需要回溯到上一个月
    $minDay=$maxDay-$misdays;
    $j=0;
    for($i=$minDay;$i<=$maxDay;$i++){
    $i=sprintf("%02d",$i);
    $maxMon = sprintf("%02d",$maxMon);
    $tmpDate=substr($maxDate,0,4)."$maxMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){
    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);
    }
    $j++;
    }

    }
    $listArr[dateArr]=$dateArr;
    $listArr[countArr]=$countArr;
    return $listArr;
    }//endTag函数getCompleteArr结束
  • 相关阅读:
    高等软工第三次作业——设计也可以按图索骥
    高等软工第二次作业-从需求分析看软件开发的挑战
    高等软工第一次作业——期望与笃信
    【ACM-ICPC 2018 徐州赛区网络预赛】D.Easy Math 杜教筛
    【HDU 6428】Calculate 莫比乌斯反演+线性筛
    【BZOJ 4199】[Noi2015]品酒大会 后缀自动机+DP
    【BZOJ 3238】差异 后缀自动机+树形DP
    【Codeforces Round #466】E. Cashback DP+ST表
    【BZOJ 4709】柠檬 斜率优化dp+单调栈
    Hello Tornado
  • 原文地址:https://www.cnblogs.com/sengling/p/5498462.html
Copyright © 2011-2022 走看看