PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 111206
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v111206
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / includes / classes / utils-arrays.inc.php

utils-arrays.inc.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 111206, at includes/classes/utils-arrays.inc.php

209 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**/
20 if (!class_exists ("c_ws_plugin__s2member_utils_arrays"))
21 {
22 /**
23 * Array utilities.
24 *
25 * @package s2Member\Utilities
26 * @since 3.5
27 */
28 class c_ws_plugin__s2member_utils_arrays
29 {
30 /**
31 * Extends ``array_unique()`` to support multi-dimensional arrays.
32 *
33 * @package s2Member\Utilities
34 * @since 3.5
35 *
36 * @param array $array Expects an incoming array.
37 * @return array Returns the ``$array`` after having reduced it to a unique set of values.
38 */
39 public static function array_unique ($array = FALSE)
40 {
41 $array = (array)$array;
42 /**/
43 foreach ($array as &$value)
44 $value = serialize ($value);
45 /**/
46 $array = array_unique ($array);
47 /**/
48 foreach ($array as &$value)
49 $value = unserialize ($value);
50 /**/
51 return $array;
52 }
53 /**
54 * Searches an array *( or even a multi-dimensional array )* using a regular expression match against array values.
55 *
56 * @package s2Member\Utilities
57 * @since 3.5
58 *
59 * @param str $regex A regular expression to look for inside the array.
60 * @return bool True if the regular expression matched at least one value in the array, else false.
61 */
62 public static function regex_in_array ($regex = FALSE, $array = FALSE)
63 {
64 if (is_string ($regex) && strlen ($regex) && is_array ($array))
65 {
66 foreach ($array as $value)
67 {
68 if (is_array ($value) /* Recursive function call? */)
69 {
70 if (c_ws_plugin__s2member_utils_arrays::regex_in_array ($regex, $value))
71 return true;
72 }
73 else if (is_string ($value) /* Must be a string. */)
74 {
75 if (@preg_match ($regex, $value))
76 return true;
77 }
78 }
79 return false;
80 }
81 else /* False. */
82 return false;
83 }
84 /**
85 * Searches an array *( or even a multi-dimensional array )* of regular expressions, to match against a string value.
86 *
87 * @package s2Member\Utilities
88 * @since 3.5
89 *
90 * @param str $string A string to test against.
91 * @param array $array An array of regex patterns to match against ``$string``.
92 * @return bool True if at least one regular expression in the ``$array`` matched ``$string``, else false.
93 */
94 public static function in_regex_array ($string = FALSE, $array = FALSE)
95 {
96 if (is_string ($string) && strlen ($string) && is_array ($array))
97 {
98 foreach ($array as $value)
99 {
100 if (is_array ($value) /* Recursive function call. */)
101 {
102 if (c_ws_plugin__s2member_utils_arrays::in_regex_array ($string, $value))
103 return true;
104 }
105 else if (is_string ($value) /* Must be a string. */)
106 {
107 if (@preg_match ($value, $string))
108 return true;
109 }
110 }
111 return false;
112 }
113 else /* False. */
114 return false;
115 }
116 /**
117 * Removes all null-value array keys from an array *( or even a multi-dimensional array )*.
118 *
119 * @package s2Member\Utilities
120 * @since 111101
121 *
122 * @param array $array An input array.
123 * @return array Returns the ``$array`` after having reduced it to a non-null set of values.
124 */
125 public static function remove_nulls ($array = FALSE)
126 {
127 $array = (array)$array;
128 /**/
129 foreach ($array as $key => &$value)
130 {
131 if (is_array ($value) /* Recursive function call here. */)
132 $value = c_ws_plugin__s2member_utils_arrays::remove_null_keys ($value);
133 /**/
134 else if (is_null ($value) /* Is it null? */)
135 unset($array[$key]);
136 }
137 return $array;
138 }
139 /**
140 * Forces string values on each array value *( also supports multi-dimensional arrays )*.
141 *
142 * @package s2Member\Utilities
143 * @since 111101
144 *
145 * @param array $array An input array.
146 * @return array Returns the ``$array`` after having forced it to set of string values.
147 */
148 public static function force_strings ($array = FALSE)
149 {
150 $array = (array)$array;
151 /**/
152 foreach ($array as &$value)
153 {
154 if (is_array ($value) /* Recursive function call here. */)
155 $value = c_ws_plugin__s2member_utils_arrays::force_strings ($value);
156 /**/
157 else if (!is_string ($value) /* String? */)
158 $value = (string)$value;
159 }
160 return $array;
161 }
162 /**
163 * Forces integer values on each array value *( also supports multi-dimensional arrays )*.
164 *
165 * @package s2Member\Utilities
166 * @since 111101
167 *
168 * @param array $array An input array.
169 * @return array Returns the ``$array`` after having forced it to set of integer values.
170 */
171 public static function force_integers ($array = FALSE)
172 {
173 $array = (array)$array;
174 /**/
175 foreach ($array as &$value)
176 {
177 if (is_array ($value) /* Recursive function call here. */)
178 $value = c_ws_plugin__s2member_utils_arrays::force_integers ($value);
179 /**/
180 else if (!is_integer ($value) /* Integer? */)
181 $value = (int)$value;
182 }
183 return $array;
184 }
185 /**
186 * Sorts arrays *( also supports multi-dimensional arrays )* by key, low to high.
187 *
188 * @package s2Member\Utilities
189 * @since 111205
190 *
191 * @param array $array An input array.
192 * @param int $flags Optional. Can be used to modify the sorting behavior.
193 * See: {@link http://www.php.net/manual/en/function.ksort.php}
194 * @return Unlike PHP's ``ksort()``, this function returns the array, and does NOT work on a reference.
195 */
196 function ksort_deep ($array = FALSE, $flags = SORT_REGULAR)
197 {
198 $array = (array)$array;
199 ksort /* Sort by key. */ ($array, $flags);
200 /**/
201 foreach ($array as &$value)
202 if (is_array ($value) /* Recursive function call here. */)
203 $value = c_ws_plugin__s2member_utils_arrays::ksort_deep ($value, $flags);
204 /**/
205 return /* Now return the array. */ $array;
206 }
207 }
208 }
209 ?>