-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLBSArray.php
132 lines (127 loc) · 3.56 KB
/
LBSArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Copyright © 2013 NEILSEN·CHAN. All rights reserved.
* Date: 2/11/13
* Description: LBSArray.php
*/
class LBSArray
{
/**
* 数组键名翻译成驼峰写法
* @author chenjinlong 20120822
* @param $in 源数组
* @return array 输出数组
*/
public function arrayKeysToCamelCase($in)
{
if(empty($in)||!is_array($in))
return $in;
$reCopyRes = array();
foreach($in as $key=>$val)
{
$reKey = LBSString::ucFirstWord($key);
if(!is_array($val)){
$reCopyRes[$reKey] = $val;
}else{
$reCopyRes[$reKey] = self::arrayKeysToCamelCase($val);
}
}
return $reCopyRes;
}
/**
* 数组键名翻译成下划线写法
* @author chenjinlong 20120822
* @param $in 源数组
* @return array 输出数组
*/
public function arrayKeysToUnderlineCase($in)
{
if(empty($in)||!is_array($in))
return $in;
$reCopyRes = array();
foreach($in as $key=>$val)
{
$reKey = LBSString::lcFirstWord($key);
if(!is_array($val)){
$reCopyRes[$reKey] = $val;
}else{
$reCopyRes[$reKey] = self::arrayKeysToUnderlineCase($val);
}
}
return $reCopyRes;
}
/**
* 自定义数组键值为指定唯一键的数值(暂仅支持二维数组)
* @author chenjinlong 20130211
* @param $srcArray
* @param string $keyStr
* @return array
* @throws ErrorException
*/
public function doCustomKeyForArray($srcArray, $keyStr = 'id')
{
$keyStr = strval($keyStr);
if(!empty($srcArray) && is_array($srcArray) && !empty($keyStr)){
$respArray = array();
foreach($srcArray as $item)
{
$respArray[$item[$keyStr]] = $item;
}
return $respArray;
}else{
throw new ErrorException('input parameter must be a not-empty array, and key string can not be empty value', 500);
}
}
/**
* @TODO 计算数组维数
* @author chenjinlong 20130211
* @param $srcArray
* @return int
* @throws ErrorException
*/
public function countArrayAxis($srcArray)
{
//if(is_array($srcArray)){
// if(!empty($srcArray)){
// if(count($srcArray, COUNT_RECURSIVE) > count($srcArray, 0)){
//
// }return true;
// }else{
// return 1;
// }
//}else{
// throw new ErrorException('input parameter must be an not-empty array with 2-axises',000000);
//}
if(count($srcArray) == count($srcArray, COUNT_RECURSIVE)){
return 1;
}else{
return 'multidimensions';
}
}
/**
* 按照键值映射关系过滤二维数组数据
*
* @author chenjinlong 20130220
* @param $srcArray
* @param $mapping
* @return array
*/
public function filterFields($srcArray, $mapping)
{
if(!empty($srcArray) && is_array($srcArray) && is_array($mapping) && !empty($mapping)){
$tgtArray = array();
foreach($srcArray as $elem)
{
$tmpArr = array();
foreach($mapping as $k => $v)
{
$tmpArr[$v] = $elem[$k];
}
$tgtArray[] = $tmpArr;
}
return $tgtArray;
}else{
return array();
}
}
}