1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class Arrays
22: {
23:
24: 25: 26:
27: final public function __construct()
28: {
29: throw new StaticClassException;
30: }
31:
32:
33:
34: 35: 36: 37: 38: 39: 40: 41:
42: public static function get(array $arr, $key, $default = NULL)
43: {
44: foreach (is_array($key) ? $key : array($key) as $k) {
45: if (is_array($arr) && array_key_exists($k, $arr)) {
46: $arr = $arr[$k];
47: } else {
48: if (func_num_args() < 3) {
49: throw new InvalidArgumentException("Missing item '$k'.");
50: }
51: return $default;
52: }
53: }
54: return $arr;
55: }
56:
57:
58:
59: 60: 61: 62: 63: 64:
65: public static function & getRef(& $arr, $key)
66: {
67: foreach (is_array($key) ? $key : array($key) as $k) {
68: if (is_array($arr) || $arr === NULL) {
69: $arr = & $arr[$k];
70: } else {
71: throw new InvalidArgumentException('Traversed item is not an array.');
72: }
73: }
74: return $arr;
75: }
76:
77:
78:
79: 80: 81: 82: 83: 84:
85: public static function mergeTree($arr1, $arr2)
86: {
87: $res = $arr1 + $arr2;
88: foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
89: if (is_array($v) && is_array($arr2[$k])) {
90: $res[$k] = self::mergeTree($v, $arr2[$k]);
91: }
92: }
93: return $res;
94: }
95:
96:
97:
98: 99: 100: 101: 102: 103:
104: public static function searchKey($arr, $key)
105: {
106: $foo = array($key => NULL);
107: return array_search(key($foo), array_keys($arr), TRUE);
108: }
109:
110:
111:
112: 113: 114: 115: 116: 117: 118:
119: public static function insertBefore(array &$arr, $key, array $inserted)
120: {
121: $offset = self::searchKey($arr, $key);
122: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
123: }
124:
125:
126:
127: 128: 129: 130: 131: 132: 133:
134: public static function insertAfter(array &$arr, $key, array $inserted)
135: {
136: $offset = self::searchKey($arr, $key);
137: $offset = $offset === FALSE ? count($arr) : $offset + 1;
138: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
139: }
140:
141:
142:
143: 144: 145: 146: 147: 148: 149:
150: public static function renameKey(array &$arr, $oldKey, $newKey)
151: {
152: $offset = self::searchKey($arr, $oldKey);
153: if ($offset !== FALSE) {
154: $keys = array_keys($arr);
155: $keys[$offset] = $newKey;
156: $arr = array_combine($keys, $arr);
157: }
158: }
159:
160:
161:
162: 163: 164: 165: 166: 167: 168:
169: public static function grep(array $arr, $pattern, $flags = 0)
170: {
171: set_error_handler(create_function('$severity, $message', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('pattern'=>$pattern)).'-1], EXTR_REFS); // preg_last_error does not return compile errors
172: restore_error_handler();
173: throw new RegexpException("$message in pattern: $pattern");
174: '));
175: $res = preg_grep($pattern, $arr, $flags);
176: restore_error_handler();
177: if (preg_last_error()) {
178: throw new RegexpException(NULL, preg_last_error(), $pattern);
179: }
180: return $res;
181: }
182:
183:
184:
185: 186: 187: 188: 189:
190: public static function flatten(array $arr)
191: {
192: $res = array();
193: array_walk_recursive($arr, create_function('$a', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('res'=>& $res)).'-1], EXTR_REFS); $res[] = $a; '));
194: return $res;
195: }
196:
197: }
198: