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