|
1
|
|
|
<?php |
|
2
|
|
|
namespace tinymeng\tools; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Name: Tool.php. |
|
6
|
|
|
* Author: JiaMeng <[email protected]> |
|
7
|
|
|
* Date: 2018/8/17 14:20 |
|
8
|
|
|
* Description: Tool.php. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class ArrayTool{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param $arr |
|
15
|
|
|
* @param $keys |
|
16
|
|
|
* @param $type |
|
17
|
|
|
* @return array |
|
18
|
|
|
*/ |
|
19
|
|
|
static public function arraySort($arr,$keys,$type='asc'){ |
|
20
|
|
|
$keysValue = $newArray = array(); |
|
21
|
|
|
foreach ($arr as $k=>$v){ |
|
22
|
|
|
if(isset($v[$keys])){ |
|
23
|
|
|
$keysValue[$k] = $v[$keys]; |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
if($type == 'asc'){ |
|
27
|
|
|
asort($keysValue); |
|
28
|
|
|
}else{ |
|
29
|
|
|
arsort($keysValue); |
|
30
|
|
|
} |
|
31
|
|
|
reset($keysValue); |
|
32
|
|
|
foreach ($keysValue as $k=>$v){ |
|
33
|
|
|
$newArray[$k] = $arr[$k]; |
|
34
|
|
|
} |
|
35
|
|
|
return array_values($newArray); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Description: 获取菜单树结构(递归-性能低) |
|
40
|
|
|
* Author: JiaMeng <[email protected]> |
|
41
|
|
|
* Updater: |
|
42
|
|
|
* @param array $list 数据list |
|
43
|
|
|
* @param string $filed 主键字段 |
|
44
|
|
|
* @param string $parentFiled 父节点字段 |
|
45
|
|
|
* @param int $parentId 父id值 |
|
46
|
|
|
* @param string $childrenName child |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getTreeStructureRecursion(array $list,string $filed='id',string $parentFiled='pid',int $parentId=0,string $childrenName = 'child') |
|
50
|
|
|
{ |
|
51
|
|
|
$result = array(); |
|
52
|
|
|
if(!empty($list)){ |
|
53
|
|
|
foreach($list as $val){ |
|
54
|
|
|
if($val[$parentFiled] == $parentId){ |
|
55
|
|
|
$val[$childrenName] = self::getTreeStructureRecursion($list,$filed,$parentFiled,$val[$filed]); |
|
56
|
|
|
if(empty($val[$childrenName])){ |
|
57
|
|
|
unset($val[$childrenName]); |
|
58
|
|
|
} |
|
59
|
|
|
$result[] = $val; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
return $result; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Description: 获取菜单树结构(性能高) |
|
68
|
|
|
* Author: JiaMeng <[email protected]> |
|
69
|
|
|
* Updater: |
|
70
|
|
|
* @param array $list 数据list |
|
71
|
|
|
* @param string $filed 主键字段 |
|
72
|
|
|
* @param string $parentFiled 父节点字段 |
|
73
|
|
|
* @param string $childrenName child |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function getTreeStructure(array $list,string $filed = 'id', string $parentFiled = 'pid', string $childrenName = 'child') |
|
77
|
|
|
{ |
|
78
|
|
|
$result = array(); |
|
79
|
|
|
foreach ($list as $value) { |
|
80
|
|
|
$result[$value[$filed]] = $value; |
|
81
|
|
|
} |
|
82
|
|
|
static $tree = array(); //格式化好的树 |
|
83
|
|
|
foreach ($result as $item) { |
|
84
|
|
|
if (isset($result[$item[$parentFiled]])) { |
|
85
|
|
|
$result[$item[$parentFiled]][$childrenName][] = &$result[$item[$filed]]; |
|
86
|
|
|
} else { |
|
87
|
|
|
$tree[] = &$result[$item[$filed]]; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $tree; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Description: 对象到数组转换 |
|
95
|
|
|
* Author: JiaMeng <[email protected]> |
|
96
|
|
|
* Updater: |
|
97
|
|
|
* @param $obj |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function objectToArray($obj){ |
|
101
|
|
|
if(!is_object($obj) && !is_array($obj)) { |
|
102
|
|
|
return $obj; |
|
103
|
|
|
} |
|
104
|
|
|
$arr = array(); |
|
105
|
|
|
foreach($obj as $k => $v){ |
|
106
|
|
|
$arr[$k] = self::objectToArray($v); |
|
107
|
|
|
} |
|
108
|
|
|
return $arr; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* 空数组转为object |
|
113
|
|
|
* 给安卓出接口时部分要求[]返回{}(数组返回字典类型) |
|
114
|
|
|
* @Author: TinyMeng <[email protected]> |
|
115
|
|
|
* @param $data |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function nullArrayToObject(&$data){ |
|
118
|
|
|
foreach ($data as $key=>&$val){ |
|
119
|
|
|
if(is_array($val)){ |
|
120
|
|
|
if(empty($val)){ |
|
121
|
|
|
settype($val,'object'); |
|
122
|
|
|
}else{ |
|
123
|
|
|
self::nullArrayToObject($val); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|