一些好用的common函数

/**
* 数组层级缩进转换
* @param array $array 源数组
* @param int $pid
* @param int $level
* @return array
*/
写无限分类用到
function array2level($array, $pid = 0, $level = 1)
{
static $list = [];
foreach ($array as $v) {
if ($v['pid'] == $pid) {
$v['level'] = $level;
$list[] = $v;
array2level($array, $v['id'], $level + 1);
}
}

return $list;
}

/**
* 构建层级(树状)数组
* @param array $array 要进行处理的一维数组,经过该函数处理后,该数组自动转为树状数组
* @param string $pid_name 父级ID的字段名
* @param string $child_key_name 子元素键名
* @return array|bool
*/
function array2tree(&$array, $pid_name = 'pid', $child_key_name = 'children')
{
$counter = array_children_count($array, $pid_name);
if (!isset($counter[0]) || $counter[0] == 0) {
return $array;
}
$tree = [];
while (isset($counter[0]) && $counter[0] > 0) {
$temp = array_shift($array);
if (isset($counter[$temp['id']]) && $counter[$temp['id']] > 0) {
array_push($array, $temp);
} else {
if ($temp[$pid_name] == 0) {
$tree[] = $temp;
} else {
$array = array_child_append($array, $temp[$pid_name], $temp, $child_key_name);
}
}
$counter = array_children_count($array, $pid_name);
}

return $tree;
}

/**
* 子元素计数器
* @param array $array
* @param int $pid
* @return array
*/
function array_children_count($array, $pid)
{
$counter = [];
foreach ($array as $item) {
$count = isset($counter[$item[$pid]]) ? $counter[$item[$pid]] : 0;
$count++;
$counter[$item[$pid]] = $count;
}

return $counter;
}

/**
* 把元素插入到对应的父元素$child_key_name字段
* @param $parent
* @param $pid
* @param $child
* @param string $child_key_name 子元素键名
* @return mixed
*/
function array_child_append($parent, $pid, $child, $child_key_name)
{
foreach ($parent as &$item) {
if ($item['id'] == $pid) {
if (!isset($item[$child_key_name]))
$item[$child_key_name] = [];
$item[$child_key_name][] = $child;
}
}

return $parent;
}

/**
* 循环删除目录和文件
* @param string $dir_name
* @return bool
*/
function delete_dir_file($dir_name)
{
$result = false;
if (is_dir($dir_name)) {
if ($handle = opendir($dir_name)) {
while (false !== ($item = readdir($handle))) {
if ($item != '.' && $item != '..') {
if (is_dir($dir_name . DS . $item)) {
delete_dir_file($dir_name . DS . $item);
} else {
unlink($dir_name . DS . $item);
}
}
}
closedir($handle);
if (rmdir($dir_name)) {
$result = true;
}
}
}

return $result;
}

/**
* 判断是否为手机访问
* @return boolean
*/
function is_mobile()
{
static $is_mobile;

if (isset($is_mobile)) {
return $is_mobile;
}

if (empty($_SERVER['HTTP_USER_AGENT'])) {
$is_mobile = false;
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false
|| strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false
) {
$is_mobile = true;
} else {
$is_mobile = false;
}

return $is_mobile;
}

/**
* 手机号格式检查
* @param string $mobile
* @return bool
*/
function check_mobile_number($mobile)
{
if (!is_numeric($mobile)) {
return false;
}
$reg = '#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#';

return preg_match($reg, $mobile) ? true : false;
}

// 楼层处理
function getClass($data,$pid=0){
$newArr=array();
foreach ($data as $key => $value) {
if ($value['pid']==$pid) {
$newArr[$value['id']]=$value;
$newArr[$value['id']]['zi']=getClass($data,$value['id']);
}
}

return $newArr;
}


function int_to_string2(&$data,$map=array('status'=>array(1=>'待支付',-1=>'删除',0=>'关闭',2=>'已支付',3=>'完成'))) {
if($data === false || $data === null ){
return $data;
}
$data = (array)$data;
foreach ($data as $key => $row){
foreach ($map as $col=>$pair){
if(isset($row[$col]) && isset($pair[$row[$col]])){
$data[$key][$col.'_text'] = $pair[$row[$col]];
}
}
}
return $data;
}

/**
* 把返回的数据集转换成Tree
* @param array $list 要转换的数据集
* @param string $pid parent标记字段
* @param string $level level标记字段
* @return array
* @author 麦当苗儿 <[email protected]>
*/
function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array();
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
}else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}

/**
* 将list_to_tree的树还原成列表
* @param array $tree 原来的树
* @param string $child 孩子节点的键
* @param string $order 排序显示的键,一般是主键 升序排列
* @param array $list 过渡用的中间数组,
* @return array 返回排过序的列表数组
* @author yangweijie <[email protected]>
*/
function tree_to_list($tree, $child = '_child', $order='id', &$list = array()){
if(is_array($tree)) {
$refer = array();
foreach ($tree as $key => $value) {
$reffer = $value;
if(isset($reffer[$child])){
unset($reffer[$child]);
tree_to_list($value[$child], $child, $order, $list);
}
$list[] = $reffer;
}
$list = list_sort_by($list, $order, $sortby='asc');
}
return $list;
}

相关推荐