1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: * @package Nette
11: */
12:
13:
14:
15: /**
16: * Array tools library.
17: *
18: * @author David Grudl
19: * @package Nette
20: */
21: final class NArrayTools
22: {
23:
24: /**
25: * Static class - cannot be instantiated.
26: */
27: final public function __construct()
28: {
29: throw new LogicException("Cannot instantiate static class " . get_class($this));
30: }
31:
32:
33:
34: /**
35: * Returns array item or $default if item is not set.
36: * Example: $val = NArrayTools::get($arr, 'i', 123);
37: * @param mixed array
38: * @param mixed key
39: * @param mixed default value
40: * @return mixed
41: */
42: public static function get(array $arr, $key, $default = NULL)
43: {
44: foreach (is_array($key) ? $key : array($key) as $k) {
45: if (is_array($arr) && array_key_exists($k, $arr)) {
46: $arr = $arr[$k];
47: } else {
48: return $default;
49: }
50: }
51: return $arr;
52: }
53:
54:
55:
56: /**
57: * Returns reference to array item or $default if item is not set.
58: * @param mixed array
59: * @param mixed key
60: * @return mixed
61: */
62: public static function & getRef(& $arr, $key)
63: {
64: foreach (is_array($key) ? $key : array($key) as $k) {
65: if (is_array($arr) || $arr === NULL) {
66: $arr = & $arr[$k];
67: } else {
68: throw new InvalidArgumentException('Traversed item is not an array.');
69: }
70: }
71: return $arr;
72: }
73:
74:
75:
76: /**
77: * Recursively appends elements of remaining keys from the second array to the first.
78: * @param array
79: * @param array
80: * @return array
81: */
82: public static function mergeTree($arr1, $arr2)
83: {
84: $res = $arr1 + $arr2;
85: foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
86: if (is_array($v) && is_array($arr2[$k])) {
87: $res[$k] = self::mergeTree($v, $arr2[$k]);
88: }
89: }
90: return $res;
91: }
92:
93:
94:
95: /**
96: * Searches the array for a given key and returns the offset if successful.
97: * @param array input array
98: * @param mixed key
99: * @return int offset if it is found, FALSE otherwise
100: */
101: public static function searchKey($arr, $key)
102: {
103: $foo = array($key => NULL);
104: return array_search(key($foo), array_keys($arr), TRUE);
105: }
106:
107:
108:
109: /**
110: * Inserts new array before item specified by key.
111: * @param array input array
112: * @param mixed key
113: * @param array inserted array
114: * @return void
115: */
116: public static function insertBefore(array &$arr, $key, array $inserted)
117: {
118: $offset = self::searchKey($arr, $key);
119: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
120: }
121:
122:
123:
124: /**
125: * Inserts new array after item specified by key.
126: * @param array input array
127: * @param mixed key
128: * @param array inserted array
129: * @return void
130: */
131: public static function insertAfter(array &$arr, $key, array $inserted)
132: {
133: $offset = self::searchKey($arr, $key);
134: $offset = $offset === FALSE ? count($arr) : $offset + 1;
135: $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
136: }
137:
138:
139:
140: /**
141: * Renames key in array.
142: * @param array
143: * @param mixed old key
144: * @param mixed new key
145: * @return void
146: */
147: public static function renameKey(array &$arr, $oldKey, $newKey)
148: {
149: $offset = self::searchKey($arr, $oldKey);
150: if ($offset !== FALSE) {
151: $keys = array_keys($arr);
152: $keys[$offset] = $newKey;
153: $arr = array_combine($keys, $arr);
154: }
155: }
156:
157: }
158: