将一个字段分成3个 php,整理几个方法
将一个线性数组转换为一个树,或者多维数组
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
if(mb_substr($arr,$i,1,"utf-8")==''){
continue;
}
$res[]=mb_substr($arr,$i,1,"utf-8"); //将单个字符存到数组当中
}
return $res;
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
七述