| 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 values 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 its 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_nulls ($value); |
| 133 |
/**/ |
| 134 |
else if (is_null /* Is it null? */ ($value)) |
| 135 |
unset ($array[$key]); |
| 136 |
} |
| 137 |
return $array; |
| 138 |
} |
| 139 |
/** |
| 140 |
* Removes all 0-byte strings from an array *( or even a multi-dimensional array )*. |
| 141 |
* |
| 142 |
* @package s2Member\Utilities |
| 143 |
* @since 111216 |
| 144 |
* |
| 145 |
* @param array $array An input array. |
| 146 |
* @return array Returns the ``$array`` after having reduced its set of values. |
| 147 |
*/ |
| 148 |
public static function remove_0b_strings ($array = FALSE) |
| 149 |
{ |
| 150 |
$array = (array)$array; |
| 151 |
/**/ |
| 152 |
foreach ($array as $key => &$value) |
| 153 |
{ |
| 154 |
if (is_array ($value) /* Recursive function call here. */) |
| 155 |
$value = c_ws_plugin__s2member_utils_arrays::remove_0b_strings ($value); |
| 156 |
/**/ |
| 157 |
else if (is_string ($value) && !strlen ($value)) |
| 158 |
unset ($array[$key]); |
| 159 |
} |
| 160 |
return $array; |
| 161 |
} |
| 162 |
/** |
| 163 |
* Forces string 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 string values. |
| 170 |
*/ |
| 171 |
public static function force_strings ($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_strings ($value); |
| 179 |
/**/ |
| 180 |
else if (!is_string ($value) /* String? */) |
| 181 |
$value = (string)$value; |
| 182 |
} |
| 183 |
return $array; |
| 184 |
} |
| 185 |
/** |
| 186 |
* Forces integer values on each array value *( also supports multi-dimensional arrays )*. |
| 187 |
* |
| 188 |
* @package s2Member\Utilities |
| 189 |
* @since 111101 |
| 190 |
* |
| 191 |
* @param array $array An input array. |
| 192 |
* @return array Returns the ``$array`` after having forced it to set of integer values. |
| 193 |
*/ |
| 194 |
public static function force_integers ($array = FALSE) |
| 195 |
{ |
| 196 |
$array = (array)$array; |
| 197 |
/**/ |
| 198 |
foreach ($array as &$value) |
| 199 |
{ |
| 200 |
if (is_array ($value) /* Recursive function call here. */) |
| 201 |
$value = c_ws_plugin__s2member_utils_arrays::force_integers ($value); |
| 202 |
/**/ |
| 203 |
else if (!is_integer ($value) /* Integer? */) |
| 204 |
$value = (int)$value; |
| 205 |
} |
| 206 |
return $array; |
| 207 |
} |
| 208 |
/** |
| 209 |
* Sorts arrays *( also supports multi-dimensional arrays )* by key, low to high. |
| 210 |
* |
| 211 |
* @package s2Member\Utilities |
| 212 |
* @since 111205 |
| 213 |
* |
| 214 |
* @param array $array An input array. |
| 215 |
* @param int $flags Optional. Can be used to modify the sorting behavior. |
| 216 |
* See: {@link http://www.php.net/manual/en/function.ksort.php} |
| 217 |
* @return Unlike PHP's ``ksort()``, this function returns the array, and does NOT work on a reference. |
| 218 |
*/ |
| 219 |
function ksort_deep ($array = FALSE, $flags = SORT_REGULAR) |
| 220 |
{ |
| 221 |
$array = (array)$array; |
| 222 |
ksort /* Sort by key. */ ($array, $flags); |
| 223 |
/**/ |
| 224 |
foreach ($array as &$value) |
| 225 |
if (is_array ($value) /* Recursive function call here. */) |
| 226 |
$value = c_ws_plugin__s2member_utils_arrays::ksort_deep ($value, $flags); |
| 227 |
/**/ |
| 228 |
return /* Now return the array. */ $array; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
?> |