| 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 |
if (!is_array ($array)) |
| 42 |
{ |
| 43 |
return array ($array); |
| 44 |
} |
| 45 |
else /* Serialized array_unique. */ |
| 46 |
{ |
| 47 |
foreach ($array as &$value) |
| 48 |
{ |
| 49 |
$value = serialize ($value); |
| 50 |
} |
| 51 |
/**/ |
| 52 |
$array = array_unique ($array); |
| 53 |
/**/ |
| 54 |
foreach ($array as &$value) |
| 55 |
{ |
| 56 |
$value = unserialize ($value); |
| 57 |
} |
| 58 |
/**/ |
| 59 |
return $array; |
| 60 |
} |
| 61 |
} |
| 62 |
/** |
| 63 |
* Searches an array *( or even a multi-dimensional array )* using a regular expression match against array values. |
| 64 |
* |
| 65 |
* @package s2Member\Utilities |
| 66 |
* @since 3.5 |
| 67 |
* |
| 68 |
* @param str $regex A regular expression to look for inside the array. |
| 69 |
* @return bool True if the regular expression matched at least one value in the array, else false. |
| 70 |
*/ |
| 71 |
public static function regex_in_array ($regex = FALSE, $array = FALSE) |
| 72 |
{ |
| 73 |
if (is_string ($regex) && strlen ($regex) && is_array ($array)) |
| 74 |
{ |
| 75 |
foreach ($array as $value) |
| 76 |
{ |
| 77 |
if (is_array ($value)) /* Recursive function call? */ |
| 78 |
{ |
| 79 |
if (c_ws_plugin__s2member_utils_arrays::regex_in_array ($regex, $value)) |
| 80 |
return true; |
| 81 |
} |
| 82 |
else if (is_string ($value)) /* Must be a string. */ |
| 83 |
{ |
| 84 |
if (@preg_match ($regex, $value)) |
| 85 |
return true; |
| 86 |
} |
| 87 |
} |
| 88 |
/**/ |
| 89 |
return false; |
| 90 |
} |
| 91 |
else /* False. */ |
| 92 |
return false; |
| 93 |
} |
| 94 |
/** |
| 95 |
* Searches an array *( or even a multi-dimensional array )* of regular expressions, to match against a string value. |
| 96 |
* |
| 97 |
* @package s2Member\Utilities |
| 98 |
* @since 3.5 |
| 99 |
* |
| 100 |
* @param str $string A string to test against. |
| 101 |
* @param array $array An array of regex patterns to match against ``$string``. |
| 102 |
* @return bool True if at least one regular expression in the ``$array`` matched ``$string``, else false. |
| 103 |
*/ |
| 104 |
public static function in_regex_array ($string = FALSE, $array = FALSE) |
| 105 |
{ |
| 106 |
if (is_string ($string) && strlen ($string) && is_array ($array)) |
| 107 |
{ |
| 108 |
foreach ($array as $value) |
| 109 |
{ |
| 110 |
if (is_array ($value)) /* Recursive function call. */ |
| 111 |
{ |
| 112 |
if (c_ws_plugin__s2member_utils_arrays::in_regex_array ($string, $value)) |
| 113 |
return true; |
| 114 |
} |
| 115 |
else if (is_string ($value)) /* Must be a string. */ |
| 116 |
{ |
| 117 |
if (@preg_match ($value, $string)) |
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
/**/ |
| 122 |
return false; |
| 123 |
} |
| 124 |
else /* False. */ |
| 125 |
return false; |
| 126 |
} |
| 127 |
/** |
| 128 |
* Removes all null-value array keys from an array *( or even a multi-dimensional array )*. |
| 129 |
* |
| 130 |
* @package s2Member\Utilities |
| 131 |
* @since 110720 |
| 132 |
* |
| 133 |
* @param array $array An input array. |
| 134 |
* @return array|mixed The output array, or whatever was passed in. |
| 135 |
*/ |
| 136 |
public static function remove_null_keys ($array = FALSE) |
| 137 |
{ |
| 138 |
if (is_array ($array) && !empty ($array)) |
| 139 |
{ |
| 140 |
foreach ($array as $key => $value) |
| 141 |
{ |
| 142 |
if (is_array ($value)) /* Recursive function call. */ |
| 143 |
{ |
| 144 |
$array[$key] = c_ws_plugin__s2member_utils_arrays::remove_null_keys ($value); |
| 145 |
} |
| 146 |
else if (is_null ($value)) /* Is it null? */ |
| 147 |
{ |
| 148 |
unset($array[$key]); |
| 149 |
} |
| 150 |
} |
| 151 |
/**/ |
| 152 |
return $array; |
| 153 |
} |
| 154 |
else /* Return same. */ |
| 155 |
return $array; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
?> |