00001 <?php
00002
00003
00004
00005
00006
00007
00008 class Arr {
00009
00010
00011
00012 public static function get_item($arr, $key, $default = false) {
00013 if (isset($arr[$key])) {
00014 return $arr[$key];
00015 }
00016 return $default;
00017 }
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 public static function get_item_recursive($arr, $key, $default = false) {
00029 $ret = $default;
00030 $arr_keys = self::extract_array_keys($key);
00031
00032 if (count($arr_keys) > 0) {
00033 $last_key = array_pop($arr_keys);
00034 foreach($arr_keys as $key) {
00035 $arr = self::get_item($arr, $key, array());
00036 }
00037 $ret = self::get_item($arr, $last_key, $default);
00038 }
00039
00040 return $ret;
00041 }
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 public static function set_item_recursive(&$arr, $key, $value) {
00053 $ret = false;
00054
00055 $arr_keys = self::extract_array_keys($key);
00056 if (count($arr_keys) == 0) {
00057 return false;
00058 }
00059
00060 $last_key = array_pop($arr_keys);
00061 $arr_work =& $arr;
00062
00063 while (is_array($arr_work)) {
00064 $cur_key = array_shift($arr_keys);
00065 if (is_null($cur_key)) {
00066 break;
00067 }
00068 if (!isset($arr_work[$cur_key])) {
00069 $arr_work[$cur_key] = array();
00070 }
00071 $arr_work =& $arr_work[$cur_key];
00072 }
00073 if (is_array($arr_work)) {
00074 $arr_work[$last_key] = $value;
00075 $ret = true;
00076 }
00077
00078 return $ret;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 public static function unset_item_recursive(&$arr, $key) {
00090 $ret = false;
00091
00092 $arr_keys = self::extract_array_keys($key);
00093 if (count($arr_keys) == 0) {
00094 return false;
00095 }
00096
00097 $last_key = array_pop($arr_keys);
00098 $arr_work =& $arr;
00099 while (is_array($arr_work)) {
00100 $cur_key = array_shift($arr_keys);
00101 if (is_null($cur_key)) {
00102 break;
00103 }
00104 if (!isset($arr_work[$cur_key])) {
00105 break;
00106 }
00107 else {
00108 $arr_work =& $arr_work[$cur_key];
00109 }
00110 }
00111 if (is_array($arr_work)) {
00112 unset($arr_work[$last_key]);
00113 $ret = true;
00114 }
00115
00116 return $ret;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 public static function extract_array_keys($key) {
00126 $ret = array();
00127 if (is_array($key)) {
00128 $ret = $key;
00129 }
00130 else {
00131 $regex = '|\[(.*?)\]|';
00132 $ret = preg_split($regex, $key, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
00133 }
00134 return $ret;
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 public static function clean(&$clean, &$source) {
00149 foreach($source as $key => $value) {
00150 if (array_key_exists($key, $clean))
00151 $clean[$key] = strip_tags($value);
00152 }
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 public static function implode($glue, $pieces, $assign = '=') {
00165 $ret = '';
00166 if (empty($assign)) {
00167 $ret = implode($glue, $pieces);
00168 }
00169 else {
00170 $bIsFirst = true;
00171 foreach($pieces as $key => $value) {
00172 if ($bIsFirst == false) {
00173 $ret .= $glue;
00174 }
00175 $bIsFirst = false;
00176 $ret .= $key . $assign . $value;
00177 }
00178 }
00179 return $ret;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 public static function implode_tail($glue, $glue_tail, $pieces) {
00207 $ret = '';
00208 $last = array_pop($pieces);
00209 $ret .= implode($glue, $pieces);
00210 if (!is_null($last)) {
00211 if ($ret !== '') {
00212 $ret .= $glue_tail;
00213 }
00214 $ret .= $last;
00215 }
00216 return $ret;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 public static function force($value, $allow_empty = true) {
00228 if (is_array($value)) {
00229 return $value;
00230 }
00231 $ret = array();
00232 if ($allow_empty || !empty($value)) {
00233 $ret[] = $value;
00234 }
00235 return $ret;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244 public static function force_keys_to_string($arr) {
00245 $ret = array();
00246 foreach($arr as $key => $value) {
00247 if (is_array($value)) {
00248 $value = self::force_keys_to_string($value);
00249 }
00250 if (is_numeric($key)) {
00251 $key = '__string_casted_' . $key;
00252 }
00253 $ret[$key] = $value;
00254 }
00255 return $ret;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 public static function unforce_keys_from_string($arr) {
00265 $ret = array();
00266 foreach($arr as $key => $value) {
00267 if (is_array($value)) {
00268 $value = self::unforce_keys_from_string($value);
00269 }
00270 $key = str_replace('__string_casted_', '', $key);
00271 $ret[$key] = $value;
00272 }
00273 return $ret;
00274 }
00275
00276
00277
00278
00279
00280
00281
00282 public static function remove(&$arr, $value) {
00283 foreach(array_keys($arr, $value) as $key) {
00284 unset($arr[$key]);
00285 }
00286 }
00287
00288
00289
00290
00291
00292
00293
00294 public static function remove_recursive(&$arr, $value) {
00295 self::remove($arr, $value);
00296 foreach($arr as $key => $item) {
00297 if (is_array($item)) {
00298 self::remove_recursive($item, $value);
00299 $arr[$key] = $item;
00300 }
00301 }
00302 }
00303 }