webman-admin/app/functions.php
2023-03-23 13:35:23 +08:00

84 lines
2.3 KiB
PHP

<?php
/**
* Here is your custom functions.
*/
if(!function_exists('substr_symbol_behind')){
/**
* @notes 截取某字符字符串
* @param $str
* @param string $symbol
* @return string
* @author 乔峰
* @date 2021/12/28 18:24
*/
function substr_symbol_behind($str, $symbol = '.') : string
{
$result = strripos($str, $symbol);
if ($result === false) {
return $str;
}
return substr($str, $result + 1);
}
}
/**
* @notes 随机生成token值
* @param string $extra
* @return string
* @author 乔峰
* @date 2021/12/28 18:24
*/
function create_token(string $extra = '') : string
{
return md5($extra . time());
}
/**
* 多级线性结构排序
* 转换前:
* [{"id":1,"pid":0,"name":"a"},{"id":2,"pid":0,"name":"b"},{"id":3,"pid":1,"name":"c"},
* {"id":4,"pid":2,"name":"d"},{"id":5,"pid":4,"name":"e"},{"id":6,"pid":5,"name":"f"},
* {"id":7,"pid":3,"name":"g"}]
* 转换后:
* [{"id":1,"pid":0,"name":"a","level":1},{"id":3,"pid":1,"name":"c","level":2},{"id":7,"pid":3,"name":"g","level":3},
* {"id":2,"pid":0,"name":"b","level":1},{"id":4,"pid":2,"name":"d","level":2},{"id":5,"pid":4,"name":"e","level":3},
* {"id":6,"pid":5,"name":"f","level":4}]
* @param array $data 线性结构数组
* @param string $symbol 名称前面加符号
* @param string $name 名称
* @param string $id_name 数组id名
* @param string $parent_id_name 数组祖先id名
* @param int $level 此值请勿给参数
* @param int $parent_id 此值请勿给参数
* @return array
*/
function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0)
{
$tree = [];
foreach ($data as $row) {
if ($row[$parent_id_name] == $parent_id) {
$temp = $row;
$child = linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
if ($child) {
$temp[$sub_key_name] = $child;
}
$tree[] = $temp;
}
}
return $tree;
}
function createDir($path){
if (is_dir($path)) {
return true;
}
$parent = dirname($path);
if (!is_dir($parent)) {
if (!createDir($parent)) {
return false;
}
}
return mkdir($path);
}