neat_array.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class NeatArray{
00038
00039
00040
00041
00042
00043
00044 var $value;
00045
00046
00047
00048
00049
00050
00051
00052 function NeatArray($value = array()) {
00053 $this->value = $value;
00054 }
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 function findIn($fieldName, $value) {
00065 if (!is_array($this->value)) {
00066 return false;
00067 }
00068 $out = false;
00069 $keys = array_keys($this->value);
00070 $count = sizeof($keys);
00071
00072 for ($i = 0; $i < $count; $i++) {
00073 if (isset($this->value[$keys[$i]][$fieldName]) && ($this->value[$keys[$i]][$fieldName] == $value))
00074 {
00075 $out[$keys[$i]] = $this->value[$keys[$i]];
00076 }
00077 }
00078 return $out;
00079 }
00080
00081
00082
00083
00084
00085
00086 function cleanup() {
00087 $out = is_array($this->value) ? array(): null;
00088 foreach ($this->value as $k => $v) {
00089 if ($v == "0") {
00090 $out[$k] = $v;
00091 } elseif ($v) {
00092 $out[$k] = $v;
00093 }
00094 }
00095 $this->value=$out;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105 function add($value) {
00106 return ($this->value = $this->plus($value)) ? true : false;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116 function plus($value) {
00117 $merge = array_merge($this->value, (is_array($value) ? $value : array($value)));
00118 return $merge;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128 function totals($sortedBy = 1, $reverse = true) {
00129 $out = array();
00130 foreach ($this->value as $val) {
00131 isset($out[$val]) ? $out[$val]++ : $out[$val] = 1;
00132 }
00133
00134 if ($sortedBy == 1) {
00135 $reverse ? arsort($out, SORT_NUMERIC) : asort($out, SORT_NUMERIC);
00136 }
00137
00138 if ($sortedBy == 2) {
00139 $reverse ? krsort($out, SORT_STRING) : ksort($out, SORT_STRING);
00140 }
00141 return $out;
00142 }
00143
00144
00145
00146
00147
00148
00149 function filter($with) {
00150 return $this->value = array_filter($this->value, $with);
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 function walk($with) {
00162 array_walk($this->value, $with);
00163 return $this->value;
00164 }
00165
00166
00167
00168
00169
00170
00171 function sprintf($template) {
00172 $count = count($this->value);
00173 for ($ii = 0; $ii < $count; $ii++) {
00174 $this->value[$ii] = sprintf($template, $this->value[$ii]);
00175 }
00176 return $this->value;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185 function extract($name) {
00186 $out = array();
00187 foreach ($this->value as $val) {
00188 if (isset($val[$name]))
00189 $out[]=$val[$name];
00190 }
00191 return $out;
00192 }
00193
00194
00195
00196
00197
00198 function unique() {
00199 $unique = array_unique($this->value);
00200 return $unique;
00201 }
00202
00203
00204
00205
00206
00207 function makeUnique() {
00208 return $this->value = array_unique($this->value);
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 function joinWith($his, $onMine, $onHis = null) {
00242 if (empty($onHis)) {
00243 $onHis = $onMine;
00244 }
00245 $his = new NeatArray($his);
00246 $out = array();
00247
00248 foreach ($this->value as $key => $val) {
00249 if ($fromHis = $his->findIn($onHis, $val[$onMine])) {
00250 list($fromHis) = array_values($fromHis);
00251 $out[$key] = array_merge($val, $fromHis);
00252 } else {
00253 $out[$key] = $val;
00254 }
00255 }
00256 return $this->value = $out;
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 function threaded($root = null, $idKey = 'id', $parentIdKey = 'parent_id', $childrenKey = 'children') {
00269 $out = array();
00270 $sizeof = sizeof($this->value);
00271
00272 for ($ii = 0; $ii < $sizeof; $ii++) {
00273 if ($this->value[$ii][$parentIdKey] == $root) {
00274 $tmp = $this->value[$ii];
00275 $tmp[$childrenKey]=isset($this->value[$ii][$idKey])
00276 ? $this->threaded($this->value[$ii][$idKey], $idKey, $parentIdKey, $childrenKey) : null;
00277 $out[] = $tmp;
00278 }
00279 }
00280 return $out;
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290 function multi_search($search_value, $the_array = null) {
00291 if ($the_array == null) {
00292 $the_array = $this->value;
00293 }
00294
00295 if (is_array($the_array)) {
00296 foreach ($the_array as $key => $value) {
00297 $result = $this->multi_search($search_value, $value);
00298
00299 if (is_array($result)) {
00300 $return = $result;
00301 array_unshift($return, $key);
00302 return $return;
00303 } elseif ($result == true) {
00304 $return[]=$key;
00305 return $return;
00306 }
00307 }
00308 return false;
00309 } else {
00310 if ($search_value == $the_array) {
00311 return true;
00312 } else {
00313 return false;
00314 }
00315 }
00316 }
00317 }
00318 ?>