Source for file ArrayTools.php

Documentation is available at ArrayTools.php

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