整理幾個方法

最閒的碼農發表於2019-08-02
function array_stack (&$a, $p = '@parent', $c = '@children')
{
  $l = $t = array();
  foreach ($a AS $key => $val):
    if (!$val[$p]) $t[$key] =& $l[$key];
    else $l[$val[$p]][$c][$key] =& $l[$key];
    $l[$key] = (array)$l[$key] + $val;
  endforeach;
  return $a = array('tree' => $t, 'leaf' => $l);
}

$node = array();
$node[1] = array('@parent' => 0, 'title' => 'I am node 1.');
$node[2] = array('@parent' => 1, 'title' => 'I am node 2.');
$node[3] = array('@parent' => 2, 'title' => 'I am node 3.');
$node[4] = array('@parent' => 1, 'title' => 'I am node 4.');
$node[5] = array('@parent' => 4, 'title' => 'I am node 5.');

print_r(array_stack($node));
function page_array($count,$page,$array,$order){
    $page=(empty($page))?'1':$page; #判斷當前頁面是否為空 如果為空就表示為第一頁面
    $start=($page-1)*$count; #計算每次分頁的開始位置
    if($order==1){
      $array=array_reverse($array);  #函式返回翻轉順序的陣列
    }
    $totals=count($array);
    $countpage=ceil($totals/$count); #計算總頁面數
    $pagedata=array();
    $pagedata=array_slice($array,$start,$count);

    return $pagedata;  #返回查詢資料
}
function array_group_by($arr, $key)
{
    $grouped = [];
    foreach ($arr as $value) {
        $grouped[$value[$key]][] = $value;
    }

    if (func_num_args() > 2) {
        $args = func_get_args();
        foreach ($grouped as $key => $value) {
            $parms = array_merge([$value], array_slice($args, 2, func_num_args()));
            $grouped[$key] = call_user_func_array('array_group_by', $parms);
        }
    }

    return $grouped;
}
function get_week($date){    //強制轉換日期格式
    $date_str=date('Y-m-d',strtotime($date));    //封裝成陣列
    $arr=explode("-", $date_str);
    $year=$arr[0];    //月,輸出2位整型,不夠2位右對齊
    $month=sprintf('%02d',$arr[1]);
    $day=sprintf('%02d',$arr[2]);    //時分秒預設賦值為0;
    $hour = $minute = $second = 0;    //轉換成時間戳
    $strap = mktime($hour,$minute,$second,$month,$day,$year);    //獲取數字型星期幾
    $number_wk=date("w",$strap);    //自定義星期陣列
    $weekArr=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");    //獲取數字對應的星期
    return $weekArr[$number_wk];
}
function timediff($begin_time,$end_time)
{
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //計算天數
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        //計算小時數
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //計算分鐘數
        $remain = $remain%3600;
        $mins = intval($remain/60);
        //計算秒數
        $secs = $remain%60;
        $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
        return $res;
}
function my_dir($dir) {
       $files = array();
       $e=mb_detect_encoding($dir,array('GB2312','GBK','UTF-8'));
       $dir = mb_convert_encoding($dir,'GBK', $e);
        if(@$handle = opendir($dir)) { //注意這裡要加一個@,不然會有warning錯誤提示:)
            while(($file = readdir($handle)) !== false) {
                if($file != ".." && $file != ".") { //排除根目錄;
                    $filename=$dir."/".$file;
                    if(is_dir($filename)) { //如果是子資料夾,就進行遞迴
                        $files[$file] =my_dir($filename);
                    } else {
                        $e=mb_detect_encoding($file,array('GB2312','GBK','UTF-8'));
                        $file = mb_convert_encoding($file,'GBK', $e);
                        $files[] = $file;
                    }
                }
            }
            closedir($handle);
            return $files;
        }
    }
 function auto_read($file, $charset='UTF-8') {
        if (!file_exists($file)) {
            return false;
        } else {
            if(filesize($file) <= 0) {
                    return false;
        }
        $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
        $handle = fopen($file,"r");
        $str = fread($handle,filesize($file));
       foreach ($list as $item) {
            $tmp = mb_convert_encoding($str, $item, $item);
            if (md5($tmp) == md5($str)) {
                         return mb_convert_encoding($str, $charset, $item);
             }
        }
                return false;
    }
function format_size($size)
{
    $unit = 'B';
    if ($size >= 1024) {
        $size /= 1024;
        $unit = 'KB';
    }
    if ($size >= 1024) {
        $size /= 1024;
        $unit = 'MB';
    }
    if ($size >= 1024) {
        $size /= 1024;
        $unit = 'GB';
    }
    if ($size >= 1024) {
        $size /= 1024;
        $unit = 'TB';
    }
    return round($size) . $unit;
}
 function split_cn($arr){
    $re=array(); //定義接受字串的陣列
    for($i=0;$i<mb_strlen($arr);$i++){
        if(mb_substr($arr,$i,1,"utf-8")==''){
            continue;
        }
        $res[]=mb_substr($arr,$i,1,"utf-8"); //將單個字元存到陣列當中
     }
    return $res;
}

相關文章