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