-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArray.php
More file actions
288 lines (265 loc) · 8.02 KB
/
Array.php
File metadata and controls
288 lines (265 loc) · 8.02 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* @description Обработчик массивов
* @author nur, Se#
* @version 0.0.4
* @changeLog
* 0.0.4 added methods convert and convertLevel2LRKeys
* 0.0.3 added methods byField and filter
* 0.0.2 added methods jut, prepareData
*/
class Evil_Array
{
/**
* @description contain operated nodes
* @var array
* @author Se#
* @version 0.0.1
*/
public static $operated = array();
/**
* @description left key, for calculating lk, rk by level
* @var int
* @author Se#
* @version 0.0.1
*/
protected static $_lk = 0;
/**
* @description right key, for calculating lk, rk by level
* @var int
* @author Se#
* @version 0.0.1
*/
protected static $_rk = 0;
/**
* Доставалка из многомерных массивов
* удобно в случае использования ини конфигов
* @param string $path
* @param array $inputArray
* @example
* $config = Zend_Registry::get('config');
* Evil_Array::get('file.upload.maxfilesize', $config);
*/
public static function get ($path, array $inputArray,$default = null, $detelminer = '.')
{
// TODO: $arrayOfPath = is_array($path) ? $path : explode($delimeter, $path);
$arrayOfPath = explode($detelminer, $path);
$value = $inputArray;
foreach ($arrayOfPath as $index)
{
if(is_array($value) && isset($value[$index]))
{
$value = $value[$index];
}
else
return $default;
}
return $value;
}
/**
* @description reformat src-array to the by-level-array.
* Ex:
* src = array(
* 0 => array('id' => 1, 'level' => 1),
* 1 => array('id' => 2, 'level' => 2),
* 2 => array('id' => 4, 'level' => 1),
* 3 => array('id' => 3. 'level' => 2)
* )
*
* result:
* array(
* 0 => array(
* 'id' => 1,
* 'children' => array(
* array(
* 'id' => 2,
* 'children' => array()
* )
* )
* ),
*
* 1 => array(
* 'id' => 4,
* 'children' => array(
* array(
* 'id' => 3,
* 'children' => array()
* )
* )
* )
* )
* @static
* @param array $src
* @param array $needed
* @param int $cl current level
* @param int $index
* @param string $lf level field
* @return array
* @author Se#
* @version 0.0.2
*/
public static function jit(array $src, array $need, $cl = 0, $i = 0, $lf = 'level', $cf = 'children')
{
$result = array();
$count = count($src);
for($i; $i < $count; $i++)
{
if(isset(self::$operated[$i]))// do not operate a row second time
continue;
if($src[$i][$lf] > $cl)// child
{
self::$operated[$i] = true;// mark the current row
$data = self::prepareData($src[$i], $need);// extract needed fields
$data[$cf] = self::jit($src, $need, $src[$i][$lf], $i+1);// get children
$result[] = $data;// save node
continue;
}
break;// if the same or next branch
}
return $result;
}
/**
* @description extract $needed fields from the $src array
* @static
* @param array $src source array
* @param array $need needed fields array(field1, field2, ...)
* @param array $r result
* @param bool $bf by field
* @return array
* @author Se#
* @version 0.0.1
*/
public static function prepareData(array $src, array $need, $r = array(), $bf = true)
{
foreach($need as $field)
{
$value = $r[$field] = isset($src[$field]) ? $src[$field] : '';
if($bf)// if by field
$r[$field] = $value;
else
$r[] = $value;
}
return $r;
}
/**
* @description make a new array($field => whole cell| data[$perField]);
* Example:
* $users = Array(
* 0 => array('id' => 2, 'login' => 'user1'),
* 1 => array('id' => 3, 'login' => 'userN')
* )
* $result = Evil_Array::byField($users, null, 'id', 'login');
*
* $result :
* array(
* 2 => 'user1',
* 3 => 'userN'
* )
*
* @static
* @param array|string $dataOrName array for operating or a table name (will fetch all)
* @param object|null $db
* @param string $field
* @param bool $perField
* @return array
* @author Se#
* @version 0.0.1
*/
public static function byField($dataOrName = array(), $db = null, $field = 'id', $perField = false)
{
$db = $db ? $db : Zend_Registry::get('db');
if(is_string($dataOrName))// name
$data = $db->fetchAll($db->select()->from(Evil_DB::scope2table($dataOrName)));
else
$data = $dataOrName;
$result = array();
$count = count($data);
for($i = 0; $i < $count; $i++)
{
$id = isset($data[$i][$field]) ? $data[$i][$field] : 0;
$result[$id] = $perField && isset($data[$i][$perField]) ? $data[$i][$perField] : $data[$i];
}
return $result;
}
/**
* @description summary for Evil_Array filters.
* Example:
* $result = Evil_Array::filter('byField', array($users, null, 'id', 'login'));//see byField method
* @static
* @param string $filterName
* @param array $args
* @return mixed|null
* @author Se#
* @version 0.0.1
*/
public static function filter($filterName,array $args)
{
if(is_string($filterName) && method_exists('Evil_Array', $filterName))
return call_user_func_array(array('Evil_Array', $filterName), $args);
return null;
}
/**
* @description factory for different types of converting.
* Ex.: Evil_Array::convert('Level2LRKeys', array($src, $needed, $curLevel, $index, $levelField));
* @static
* @param string $type
* @param array $args
* @return mixed|null
* @author Se#
* @version 0.0.1
*/
public static function convert($type, array $args)
{
if(!is_string($type))
return null;
$method = 'convert' . $type;
return method_exists('Evil_Array', $method) ? call_user_func_array(array('Evil_Array', $method), $args) : null;
}
/**
* @static
* @param array $src source array
* @param array $needed needed fields
* @param int $curLevel
* @param int $index
* @param string $levelField where from level should be extracted
* @return array
* @author Se#
* @version 0.0.1
*/
public static function convertLevel2LRKeys(array $src, array $needed, $curLevel = 0, $index = 0, $levelField = 'level')
{
$result = array();
$count = count($src);
for($i = $index; $i < $count; $i++)
{
if(isset(self::$operated[$i]))// do not operate a row second time
continue;
if($src[$i][$levelField] > $curLevel)// child
{
self::$operated[$i] = true;
//$prepared = self::prepareData($src[$i], $needed);
self::$_lk++;
self::$_rk = self::$_lk+1;
$result[$i] = $src[$i];
$result[$i]['lk'] = self::$_lk;
$result[$i]['rk'] = self::$_rk;
// get children for a child
$result += self::convertLevel2LRKeys($src, $needed, $src[$i][$levelField], $i+1);
if(isset($result[$i+1]))
{
self::$_rk++;
$result[$i]['rk'] = self::$_rk;
self::$_lk = self::$_rk;
}
else
self::$_lk++;
continue;
}
elseif($src[$i][$levelField] < $curLevel)// new branch
return $result;
elseif($i)// the same branch
return $result;
}
return $result;
}
}