PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/s2member/includes/classes/utils-arrays.inc.php

https://gitlab.com/Gashler/dp
PHP | 232 lines | 121 code | 15 blank | 96 comment | 34 complexity | a36b49a71facc92b0e2cb2d04ac2406e MD5 | raw file
  1. <?php
  2. /**
  3. * Array utilities.
  4. *
  5. * Copyright: © 2009-2011
  6. * {@link http://www.websharks-inc.com/ WebSharks, Inc.}
  7. * (coded in the USA)
  8. *
  9. * Released under the terms of the GNU General Public License.
  10. * You should have received a copy of the GNU General Public License,
  11. * along with this software. In the main directory, see: /licensing/
  12. * If not, see: {@link http://www.gnu.org/licenses/}.
  13. *
  14. * @package s2Member\Utilities
  15. * @since 3.5
  16. */
  17. if (realpath (__FILE__) === realpath ($_SERVER["SCRIPT_FILENAME"]))
  18. exit ("Do not access this file directly.");
  19. if (!class_exists ("c_ws_plugin__s2member_utils_arrays"))
  20. {
  21. /**
  22. * Array utilities.
  23. *
  24. * @package s2Member\Utilities
  25. * @since 3.5
  26. */
  27. class c_ws_plugin__s2member_utils_arrays
  28. {
  29. /**
  30. * Extends ``array_unique()`` to support multi-dimensional arrays.
  31. *
  32. * @package s2Member\Utilities
  33. * @since 3.5
  34. *
  35. * @param array $array Expects an incoming array.
  36. * @return array Returns the ``$array`` after having reduced it to a unique set of values.
  37. */
  38. public static function array_unique ($array = FALSE)
  39. {
  40. $array = (array)$array;
  41. foreach ($array as &$value)
  42. $value = serialize ($value);
  43. $array = array_unique ($array);
  44. foreach ($array as &$value)
  45. $value = unserialize ($value);
  46. return $array;
  47. }
  48. /**
  49. * Searches an array *(or even a multi-dimensional array)* using a regular expression match against array values.
  50. *
  51. * @package s2Member\Utilities
  52. * @since 3.5
  53. *
  54. * @param str $regex A regular expression to look for inside the array.
  55. * @return bool True if the regular expression matched at least one value in the array, else false.
  56. */
  57. public static function regex_in_array ($regex = FALSE, $array = FALSE)
  58. {
  59. if (is_string ($regex) && strlen ($regex) && is_array ($array))
  60. {
  61. foreach ($array as $value)
  62. {
  63. if (is_array ($value) /* Recursive function call? */)
  64. {
  65. if (c_ws_plugin__s2member_utils_arrays::regex_in_array ($regex, $value))
  66. return true;
  67. }
  68. else if (is_string ($value) /* Must be a string. */)
  69. {
  70. if (@preg_match ($regex, $value))
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. else // False.
  77. return false;
  78. }
  79. /**
  80. * Searches an array *(or even a multi-dimensional array)* of regular expressions, to match against a string value.
  81. *
  82. * @package s2Member\Utilities
  83. * @since 3.5
  84. *
  85. * @param str $string A string to test against.
  86. * @param array $array An array of regex patterns to match against ``$string``.
  87. * @return bool True if at least one regular expression in the ``$array`` matched ``$string``, else false.
  88. */
  89. public static function in_regex_array ($string = FALSE, $array = FALSE)
  90. {
  91. if (is_string ($string) && strlen ($string) && is_array ($array))
  92. {
  93. foreach ($array as $value)
  94. {
  95. if (is_array ($value) /* Recursive function call. */)
  96. {
  97. if (c_ws_plugin__s2member_utils_arrays::in_regex_array ($string, $value))
  98. return true;
  99. }
  100. else if (is_string ($value) /* Must be a string. */)
  101. {
  102. if (@preg_match ($value, $string))
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. else // False.
  109. return false;
  110. }
  111. /**
  112. * Removes all null values from an array *(or even a multi-dimensional array)*.
  113. *
  114. * @package s2Member\Utilities
  115. * @since 111101
  116. *
  117. * @param array $array An input array.
  118. * @return array Returns the ``$array`` after having reduced its set of values.
  119. */
  120. public static function remove_nulls ($array = FALSE)
  121. {
  122. $array = (array)$array;
  123. foreach ($array as $key => &$value)
  124. {
  125. if (is_array ($value) /* Recursive function call here. */)
  126. $value = c_ws_plugin__s2member_utils_arrays::remove_nulls ($value);
  127. else if (is_null /* Is it null? */ ($value))
  128. unset ($array[$key]);
  129. }
  130. return $array;
  131. }
  132. /**
  133. * Removes all 0-byte strings from an array *(or even a multi-dimensional array)*.
  134. *
  135. * @package s2Member\Utilities
  136. * @since 111216
  137. *
  138. * @param array $array An input array.
  139. * @return array Returns the ``$array`` after having reduced its set of values.
  140. */
  141. public static function remove_0b_strings ($array = FALSE)
  142. {
  143. $array = (array)$array;
  144. foreach ($array as $key => &$value)
  145. {
  146. if (is_array ($value) /* Recursive function call here. */)
  147. $value = c_ws_plugin__s2member_utils_arrays::remove_0b_strings ($value);
  148. else if (is_string ($value) && !strlen ($value))
  149. unset ($array[$key]);
  150. }
  151. return $array;
  152. }
  153. /**
  154. * Forces string values on each array value *(also supports multi-dimensional arrays)*.
  155. *
  156. * @package s2Member\Utilities
  157. * @since 111101
  158. *
  159. * @param array $array An input array.
  160. * @return array Returns the ``$array`` after having forced it to set of string values.
  161. */
  162. public static function force_strings ($array = FALSE)
  163. {
  164. $array = (array)$array;
  165. foreach ($array as &$value)
  166. {
  167. if (is_array ($value) /* Recursive function call here. */)
  168. $value = c_ws_plugin__s2member_utils_arrays::force_strings ($value);
  169. else if (!is_string ($value) /* String? */)
  170. $value = (string)$value;
  171. }
  172. return $array;
  173. }
  174. /**
  175. * Forces integer values on each array value *(also supports multi-dimensional arrays)*.
  176. *
  177. * @package s2Member\Utilities
  178. * @since 111101
  179. *
  180. * @param array $array An input array.
  181. * @return array Returns the ``$array`` after having forced it to set of integer values.
  182. */
  183. public static function force_integers ($array = FALSE)
  184. {
  185. $array = (array)$array;
  186. foreach ($array as &$value)
  187. {
  188. if (is_array ($value) /* Recursive function call here. */)
  189. $value = c_ws_plugin__s2member_utils_arrays::force_integers ($value);
  190. else if (!is_integer ($value) /* Integer? */)
  191. $value = (int)$value;
  192. }
  193. return $array;
  194. }
  195. /**
  196. * Sorts arrays *(also supports multi-dimensional arrays)* by key, low to high.
  197. *
  198. * @package s2Member\Utilities
  199. * @since 111205
  200. *
  201. * @param array $array An input array.
  202. * @param int $flags Optional. Can be used to modify the sorting behavior.
  203. * See: {@link http://www.php.net/manual/en/function.ksort.php}
  204. * @return Unlike PHP's ``ksort()``, this function returns the array, and does NOT work on a reference.
  205. */
  206. function ksort_deep ($array = FALSE, $flags = SORT_REGULAR)
  207. {
  208. $array = (array)$array;
  209. ksort /* Sort by key. */ ($array, $flags);
  210. foreach ($array as &$value)
  211. if (is_array ($value) /* Recursive function call here. */)
  212. $value = c_ws_plugin__s2member_utils_arrays::ksort_deep ($value, $flags);
  213. return /* Now return the array. */ $array;
  214. }
  215. }
  216. }
  217. ?>