-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigUtils.php
More file actions
59 lines (51 loc) · 1.5 KB
/
Copy pathConfigUtils.php
File metadata and controls
59 lines (51 loc) · 1.5 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
<?php
class ConfigUtils {
public static function array_merge_recursive_drill() {
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base); //important
while (list($key, $value) = @each($array)) {
if (is_array($value) && @is_array($base[$key])) {
$base[$key] = self::array_merge_recursive_drill($base[$key], $value);
} else {
$base[$key] = $value;
}
}
}
return $base;
}
public static function insert_using_keys($keys, $value){
$array = array();
$ref = &$array;
while(count($keys) > 0){
$n = array_shift($keys);
if(!is_array($ref)) {
$ref = array();
}
$ref = &$ref[$n];
}
$ref = $value;
return $array;
}
public static function buildArray($strings, $delimeter = '.') {
$list = array();
foreach($strings as $key => $value) {
$list[] = self::insert_using_keys(explode($delimeter, $key), $value);
}
return call_user_func_array('self::array_merge_recursive_drill', $list); // as of PHP 5.3.0
}
public static function buildString($array, $delimeter = '.') {
$results = array();
$func = function($item, $key, $old_key = NULL) use (&$func, &$results, $delimeter) {
if(is_array($item)) {
array_walk($item, $func, $old_key . $key . $delimeter);
} else {
$results[$old_key . $key] = $item;
}
};
array_walk($array, $func);
return $results;
}
}
?>