基本形式
/**
* 无限分类
*
* @author Messiah
* @version v1.0.0
* @return array
*/
private function buildTree(Array $arr,string $parentName='pid',int $parentId = 0,int $level = 1) {
$tree = array();
foreach ($arr as $key => $val) {
if ($val[$parentName] == $parentId) {
$val['level'] = $level;
$val['children'] = $this->buildTree($arr, $parentName, $val['id'], $level + 1);
$tree[] = $val;
}
}
return $tree;
}
应用示例
/**
* 文件大小
*
* @author Messiah
* @version v1.0.0
* @return string
*/
private function getSize(int $size = 0,int $unit = 0)
{
$arr = ["B","KB","MB","GB","TB","PB","EB","ZB","YB","BB","NB","DB"];
if($size){
if( ($size / 1024) < 1 ){
return $size . $arr[$unit];
}
if( ($size % 1024) > 0 ){
return $this->getSize(intdiv($size,1024),$unit+1);
}else{
return intdiv($size,1024) . $arr[$unit+1]??"超过最大单位";
}
}
else{
return $size . $arr[$unit];
}
}
JS中获取文件大小
var size = parseFloat(value);
var i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(i < 2 ? 0 : 2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];