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: 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: public static function & getRef(& $arr, $key)
58: {
59: foreach (is_array($key) ? $key : array($key) as $k) {
60: if (is_array($arr) || $arr === NULL) {
61: $arr = & $arr[$k];
62: } else {
63: throw new InvalidArgumentException('Traversed item is not an array.');
64: }
65: }
66: return $arr;
67: }
68:
69:
70: 71: 72: 73:
74: public static function mergeTree($arr1, $arr2)
75: {
76: $res = $arr1 + $arr2;
77: foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
78: if (is_array($v) && is_array($arr2[$k])) {
79: $res[$k] = self::mergeTree($v, $arr2[$k]);
80: }
81: }
82: return $res;
83: }
84:
85:
86: 87: 88: 89:
90: public static function searchKey($arr, $key)
91: {
92: $foo = array($key => NULL);
93: return array_search(key($foo), array_keys($arr), TRUE);
94: }
95:
96:
97: 98: 99: 100:
101: public static function insertBefore(array & $arr, $key, array $inserted)
102: {
103: $offset = self::searchKey($arr, $key);
104: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
105: }
106:
107:
108: 109: 110: 111:
112: public static function insertAfter(array & $arr, $key, array $inserted)
113: {
114: $offset = self::searchKey($arr, $key);
115: $offset = $offset === FALSE ? count($arr) : $offset + 1;
116: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
117: }
118:
119:
120: 121: 122: 123:
124: public static function renameKey(array & $arr, $oldKey, $newKey)
125: {
126: $offset = self::searchKey($arr, $oldKey);
127: if ($offset !== FALSE) {
128: $keys = array_keys($arr);
129: $keys[$offset] = $newKey;
130: $arr = array_combine($keys, $arr);
131: }
132: }
133:
134:
135: 136: 137: 138:
139: public static function grep(array $arr, $pattern, $flags = 0)
140: {
141: 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
142: restore_error_handler();
143: throw new RegexpException("$message in pattern: $pattern");
144: '));
145: $res = preg_grep($pattern, $arr, $flags);
146: restore_error_handler();
147: if (preg_last_error()) {
148: throw new RegexpException(NULL, preg_last_error(), $pattern);
149: }
150: return $res;
151: }
152:
153:
154: 155: 156: 157:
158: public static function flatten(array $arr)
159: {
160: $res = array();
161: array_walk_recursive($arr, create_function('$a', 'extract($GLOBALS[0]['.array_push($GLOBALS[0], array('res'=>& $res)).'-1], EXTR_REFS); $res[] = $a; '));
162: return $res;
163: }
164:
165: }
166: