Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • ArrayHash
  • ArrayList
  • Arrays
  • Callback
  • CallbackFilterIterator
  • DateTime
  • FileSystem
  • Finder
  • Html
  • Image
  • Json
  • LimitedScope
  • MimeTypeDetector
  • ObjectMixin
  • Paginator
  • Random
  • RecursiveCallbackFilterIterator
  • Strings
  • TokenIterator
  • Tokenizer
  • Validators

Interfaces

  • IHtmlString

Exceptions

  • AssertionException
  • JsonException
  • RegexpException
  • TokenizerException
  • UnknownImageFileException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Utils;
  9: 
 10: use Nette;
 11: 
 12: 
 13: /**
 14:  * Array tools library.
 15:  *
 16:  * @author     David Grudl
 17:  */
 18: class Arrays
 19: {
 20: 
 21:     /**
 22:      * Static class - cannot be instantiated.
 23:      */
 24:     final public function __construct()
 25:     {
 26:         throw new Nette\StaticClassException;
 27:     }
 28: 
 29: 
 30:     /**
 31:      * Returns item from array or $default if item is not set.
 32:      * @return mixed
 33:      */
 34:     public static function get(array $arr, $key, $default = NULL)
 35:     {
 36:         foreach (is_array($key) ? $key : array($key) as $k) {
 37:             if (is_array($arr) && array_key_exists($k, $arr)) {
 38:                 $arr = $arr[$k];
 39:             } else {
 40:                 if (func_num_args() < 3) {
 41:                     throw new Nette\InvalidArgumentException("Missing item '$k'.");
 42:                 }
 43:                 return $default;
 44:             }
 45:         }
 46:         return $arr;
 47:     }
 48: 
 49: 
 50:     /**
 51:      * Returns reference to array item.
 52:      * @return mixed
 53:      */
 54:     public static function & getRef(& $arr, $key)
 55:     {
 56:         foreach (is_array($key) ? $key : array($key) as $k) {
 57:             if (is_array($arr) || $arr === NULL) {
 58:                 $arr = & $arr[$k];
 59:             } else {
 60:                 throw new Nette\InvalidArgumentException('Traversed item is not an array.');
 61:             }
 62:         }
 63:         return $arr;
 64:     }
 65: 
 66: 
 67:     /**
 68:      * Recursively appends elements of remaining keys from the second array to the first.
 69:      * @return array
 70:      */
 71:     public static function mergeTree($arr1, $arr2)
 72:     {
 73:         $res = $arr1 + $arr2;
 74:         foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
 75:             if (is_array($v) && is_array($arr2[$k])) {
 76:                 $res[$k] = self::mergeTree($v, $arr2[$k]);
 77:             }
 78:         }
 79:         return $res;
 80:     }
 81: 
 82: 
 83:     /**
 84:      * Searches the array for a given key and returns the offset if successful.
 85:      * @return int    offset if it is found, FALSE otherwise
 86:      */
 87:     public static function searchKey($arr, $key)
 88:     {
 89:         $foo = array($key => NULL);
 90:         return array_search(key($foo), array_keys($arr), TRUE);
 91:     }
 92: 
 93: 
 94:     /**
 95:      * Inserts new array before item specified by key.
 96:      * @return void
 97:      */
 98:     public static function insertBefore(array & $arr, $key, array $inserted)
 99:     {
100:         $offset = self::searchKey($arr, $key);
101:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
102:     }
103: 
104: 
105:     /**
106:      * Inserts new array after item specified by key.
107:      * @return void
108:      */
109:     public static function insertAfter(array & $arr, $key, array $inserted)
110:     {
111:         $offset = self::searchKey($arr, $key);
112:         $offset = $offset === FALSE ? count($arr) : $offset + 1;
113:         $arr = array_slice($arr, 0, $offset, TRUE) + $inserted + array_slice($arr, $offset, count($arr), TRUE);
114:     }
115: 
116: 
117:     /**
118:      * Renames key in array.
119:      * @return void
120:      */
121:     public static function renameKey(array & $arr, $oldKey, $newKey)
122:     {
123:         $offset = self::searchKey($arr, $oldKey);
124:         if ($offset !== FALSE) {
125:             $keys = array_keys($arr);
126:             $keys[$offset] = $newKey;
127:             $arr = array_combine($keys, $arr);
128:         }
129:     }
130: 
131: 
132:     /**
133:      * Returns array entries that match the pattern.
134:      * @return array
135:      */
136:     public static function grep(array $arr, $pattern, $flags = 0)
137:     {
138:         return Strings::pcre('preg_grep', array($pattern, $arr, $flags));
139:     }
140: 
141: 
142:     /**
143:      * Returns flattened array.
144:      * @return array
145:      */
146:     public static function flatten(array $arr, $preserveKeys = FALSE)
147:     {
148:         $res = array();
149:         $cb = $preserveKeys
150:             ? function($v, $k) use (& $res) { $res[$k] = $v; }
151:             : function($v) use (& $res) { $res[] = $v; };
152:         array_walk_recursive($arr, $cb);
153:         return $res;
154:     }
155: 
156: 
157:     /**
158:      * Finds whether a variable is a zero-based integer indexed array.
159:      * @return bool
160:      */
161:     public static function isList($value)
162:     {
163:         return is_array($value) && (!$value || array_keys($value) === range(0, count($value) - 1));
164:     }
165: 
166: 
167:     /**
168:      * Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
169:      * @return array|stdClass
170:      */
171:     public static function associate(array $arr, $path)
172:     {
173:         $parts = is_array($path)
174:             ? $path
175:             : preg_split('#(\[\]|->|=|\|)#', $path, NULL, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
176: 
177:         if (!$parts || $parts[0] === '=' || $parts[0] === '|' || $parts === array('->')) {
178:             throw new Nette\InvalidArgumentException("Invalid path '$path'.");
179:         }
180: 
181:         $res = $parts[0] === '->' ? new \stdClass : array();
182: 
183:         foreach ($arr as $rowOrig) {
184:             $row = (array) $rowOrig;
185:             $x = & $res;
186: 
187:             for ($i = 0; $i < count($parts); $i++) {
188:                 $part = $parts[$i];
189:                 if ($part === '[]') {
190:                     $x = & $x[];
191: 
192:                 } elseif ($part === '=') {
193:                     if (isset($parts[++$i])) {
194:                         $x = $row[$parts[$i]];
195:                         $row = NULL;
196:                     }
197: 
198:                 } elseif ($part === '->') {
199:                     if (isset($parts[++$i])) {
200:                         $x = & $x->{$row[$parts[$i]]};
201:                     } else {
202:                         $row = is_object($rowOrig) ? $rowOrig : (object) $row;
203:                     }
204: 
205:                 } elseif ($part !== '|') {
206:                     $x = & $x[(string) $row[$part]];
207:                 }
208:             }
209: 
210:             if ($x === NULL) {
211:                 $x = $row;
212:             }
213:         }
214: 
215:         return $res;
216:     }
217: 
218: }
219: 
Nette 2.2.6 API API documentation generated by ApiGen 2.8.0