Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

Exceptions

  • Overview
  • Package
  • 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:  * @package Nette\Utils
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Array tools library.
 13:  *
 14:  * @author     David Grudl
 15:  * @package Nette\Utils
 16:  */
 17: class NArrays
 18: {
 19: 
 20:     /**
 21:      * Static class - cannot be instantiated.
 22:      */
 23:     final public function __construct()
 24:     {
 25:         throw new NStaticClassException;
 26:     }
 27: 
 28: 
 29:     /**
 30:      * Returns item from array or $default if item is not set.
 31:      * @return mixed
 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:      * Returns reference to array item or $default if item is not set.
 51:      * @return mixed
 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:      * Recursively appends elements of remaining keys from the second array to the first.
 68:      * @return array
 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:      * Searches the array for a given key and returns the offset if successful.
 84:      * @return int    offset if it is found, FALSE otherwise
 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:      * Inserts new array before item specified by key.
 95:      * @return void
 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:      * Inserts new array after item specified by key.
106:      * @return void
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:      * Renames key in array.
118:      * @return void
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:      * Returns array entries that match the pattern.
133:      * @return array
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 NRegexpException("$message in pattern: $pattern");
140:         '));
141:         $res = preg_grep($pattern, $arr, $flags);
142:         restore_error_handler();
143:         if (preg_last_error()) { // run-time error
144:             throw new NRegexpException(NULL, preg_last_error(), $pattern);
145:         }
146:         return $res;
147:     }
148: 
149: 
150:     /**
151:      * Returns flattened array.
152:      * @return array
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: 
Nette Framework 2.0.15 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0