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