@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * Description: Tool.php. |
9 | 9 | */ |
10 | 10 | |
11 | -class ArrayTool{ |
|
11 | +class ArrayTool { |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * @param $arr |
@@ -16,20 +16,20 @@ discard block |
||
16 | 16 | * @param $type |
17 | 17 | * @return array |
18 | 18 | */ |
19 | - static public function arraySort($arr,$keys,$type='asc'){ |
|
19 | + static public function arraySort($arr, $keys, $type = 'asc') { |
|
20 | 20 | $keysValue = $newArray = array(); |
21 | - foreach ($arr as $k=>$v){ |
|
22 | - if(isset($v[$keys])){ |
|
21 | + foreach ($arr as $k=>$v) { |
|
22 | + if (isset($v[$keys])) { |
|
23 | 23 | $keysValue[$k] = $v[$keys]; |
24 | 24 | } |
25 | 25 | } |
26 | - if($type == 'asc'){ |
|
26 | + if ($type == 'asc') { |
|
27 | 27 | asort($keysValue); |
28 | - }else{ |
|
28 | + } else { |
|
29 | 29 | arsort($keysValue); |
30 | 30 | } |
31 | 31 | reset($keysValue); |
32 | - foreach ($keysValue as $k=>$v){ |
|
32 | + foreach ($keysValue as $k=>$v) { |
|
33 | 33 | $newArray[$k] = $arr[$k]; |
34 | 34 | } |
35 | 35 | return array_values($newArray); |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | * @param string $childrenName child |
47 | 47 | * @return array |
48 | 48 | */ |
49 | - public static function getTreeStructureRecursion(array $list,string $filed='id',string $parentFiled='pid',int $parentId=0,string $childrenName = 'child') |
|
49 | + public static function getTreeStructureRecursion(array $list, string $filed = 'id', string $parentFiled = 'pid', int $parentId = 0, string $childrenName = 'child') |
|
50 | 50 | { |
51 | 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])){ |
|
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 | 57 | unset($val[$childrenName]); |
58 | 58 | } |
59 | 59 | $result[] = $val; |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | * @param string $childrenName child |
74 | 74 | * @return array |
75 | 75 | */ |
76 | - public static function getTreeStructure(array $list,string $filed = 'id', string $parentFiled = 'pid', string $childrenName = 'child') |
|
76 | + public static function getTreeStructure(array $list, string $filed = 'id', string $parentFiled = 'pid', string $childrenName = 'child') |
|
77 | 77 | { |
78 | 78 | $result = array(); |
79 | 79 | foreach ($list as $value) { |
@@ -97,12 +97,12 @@ discard block |
||
97 | 97 | * @param $obj |
98 | 98 | * @return array |
99 | 99 | */ |
100 | - public static function objectToArray($obj){ |
|
101 | - if(!is_object($obj) && !is_array($obj)) { |
|
100 | + public static function objectToArray($obj) { |
|
101 | + if (!is_object($obj) && !is_array($obj)) { |
|
102 | 102 | return $obj; |
103 | 103 | } |
104 | 104 | $arr = array(); |
105 | - foreach($obj as $k => $v){ |
|
105 | + foreach ($obj as $k => $v) { |
|
106 | 106 | $arr[$k] = self::objectToArray($v); |
107 | 107 | } |
108 | 108 | return $arr; |
@@ -114,12 +114,12 @@ discard block |
||
114 | 114 | * @Author: TinyMeng <[email protected]> |
115 | 115 | * @param $data |
116 | 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{ |
|
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 | 123 | self::nullArrayToObject($val); |
124 | 124 | } |
125 | 125 | } |