PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 111029
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v111029
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 111029, at includes/classes/utils-arrays.inc.php

147 lines 4.3 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 /**/
80 return false;
81 }
82 else /* False. */
83 return false;
84 }
85 /**
86 * Searches an array *( or even a multi-dimensional array )* of regular expressions, to match against a string value.
87 *
88 * @package s2Member\Utilities
89 * @since 3.5
90 *
91 * @param str $string A string to test against.
92 * @param array $array An array of regex patterns to match against ``$string``.
93 * @return bool True if at least one regular expression in the ``$array`` matched ``$string``, else false.
94 */
95 public static function in_regex_array ($string = FALSE, $array = FALSE)
96 {
97 if (is_string ($string) && strlen ($string) && is_array ($array))
98 {
99 foreach ($array as $value)
100 {
101 if (is_array ($value)) /* Recursive function call. */
102 {
103 if (c_ws_plugin__s2member_utils_arrays::in_regex_array ($string, $value))
104 return true;
105 }
106 else if (is_string ($value)) /* Must be a string. */
107 {
108 if (@preg_match ($value, $string))
109 return true;
110 }
111 }
112 /**/
113 return false;
114 }
115 else /* False. */
116 return false;
117 }
118 /**
119 * Removes all null-value array keys from an array *( or even a multi-dimensional array )*.
120 *
121 * @package s2Member\Utilities
122 * @since 110720
123 *
124 * @param array $array An input array.
125 * @return array|mixed The output array, or whatever was passed in.
126 */
127 public static function remove_null_keys ($array = FALSE)
128 {
129 if (is_array ($array) && !empty ($array))
130 {
131 foreach ($array as $key => &$value)
132 {
133 if (is_array ($value)) /* Recursive function call here. */
134 $value = c_ws_plugin__s2member_utils_arrays::remove_null_keys ($value);
135 /**/
136 else if (is_null ($value)) /* Is it null? */
137 unset ($array[$key]);
138 }
139 /**/
140 return $array;
141 }
142 else /* Return same. */
143 return $array;
144 }
145 }
146 }
147 ?>