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