[go: up one dir, main page]

Menu

[r19]: / tags / 0.58 / lib / arrays.php  Maximize  Restore  History

Download this file

312 lines (198 with data), 6.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/******************************************************************
This file contains all my specialty array functions
Modified 04-18-2005.
Added arrayStringSearch function
Modified 04-20-2005
added reduceArray function
Modified 09-12-2005
Modified reduceArray to handle associative arrays
******************************************************************/
function cs_in_array($string,$object) {
$string = strtolower($string);
$returnValue = null;
for ($row=0;$row<count($object);$row++) {
$tempValue = strtolower($object[$row]);
if ($tempValue == $string) $returnValue = $tempValue; break;
}
if ($returnValue) return $returnValue;
else return false;
}
function multi_array_unique($array1,$array2) {
$newArray1 = array();
$newArray2 = array();
for ($row=0;$row<count($array1);$row++) {
if (!in_array($array1[$row],$newArray1)) {
$newArray1[] = $array1[$row];
$newArray2[] = $array2[$row];
}
}
return array($newArray1,$newArray2);
}
//This function compares two arrays, and returns an array w/ the differences
function array_difference($array1,$array2) {
$newrow=0;
$final_array = array();
for ($row=0;$row<count($array1);$row++) {
if (!in_array($array1[$row],$array2)) {
$final_array[$newrow]=$array1[$row];
$newrow++;
}
}
return $final_array;
}
//creates an array from an array w/ the specified keys
function createKeyArray($arr,$keys) {
$new = array();
$num = count($keys);
for ($row=0;$row<$num;$row++) {
$key = $keys[$row];
$new[] = $arr[$key];
}
return $new;
}
//creates an array from an array w/ the specified keys
function createAssocKeyArray($arr,$keys) {
$new = array();
$num = count($keys);
for ($row=0;$row<$num;$row++) {
$key = $keys[$row];
if (!is_numeric($key)) $new[$key] = $arr[$key];
}
return $new;
}
/*************************************************
this function returns all information
for an id from an array, similar to the way
single_result returns info from a database
*************************************************/
function returnObjectInfo($arr,$id) {
if (!is_array($arr)) return false;
$key = array_search($id,$arr["id"]);
$fields = array_keys($arr);
$num = count($fields);
$val = array();
for ($row=0;$row<$num;$row++) {
$field = $fields[$row];
$val[$field] = $arr[$field][$key];
}
return $val;
}
//creates an array out of one array containing
//keys, and another containing array values.
//should be replaced by array_combine in php5
if (!function_exists("array_combine")) {
function array_combine($keyArray,$valArray) {
$arr = array();
$num = count($keyArray);
for ($row=0;$row<$num;$row++) {
$key = $keyArray[$row];
$arr[] = $valArray[$key];
}
return $arr;
}
}
function arrayCombine($keyArray,$valArray) {
$arr = array();
$num = count($keyArray);
for ($row=0;$row<$num;$row++) {
$key = $keyArray[$row];
$arr[] = $valArray[$key];
}
return $arr;
}
function transposeArray($arr) {
foreach ($arr AS $keymaster => $value) {
foreach($value AS $key => $element) {
$returnArray[$key][$keymaster] = $element;
}
}
return $returnArray;
}
/******************************************************
arrayMultiSort
-Sort an associative array by a key in that
array
-modArray is the array we are sorting
-sort is the key we sort by
-dir is either ASC or DESC, and is optional.
it's the direction of our sort
******************************************************/
function arrayMultiSort($modArray,$sort,$dir="ASC") {
$newArray = array();
//parameter sanity checking
if (!$sort) return false;
if (!is_array($modArray)) return false;
//sort by our sort field
$arr = $modArray[$sort];
if (!is_array($arr)) return false;
//get the keys in the array. We will use these as field names later
$fields = array_keys($modArray);
//we will assume the first key in the array is the index key. That means
//there should be one of these in every array entry. This way, if some of
//our sort fields are empty, we can pad the sort array to the required length
$idx = $fields[0];
$realSize = count($modArray[$idx]);
//sort our array
if ($dir=="DESC") arsort($arr);
else asort($arr);
//the size of our sorted field
$sortSize = count($arr);
//if our sort size is smaller than our actual size, pad to the real size so we don't lose any modules.
//the value we pad it with should ensure it gets placed last whether it's a numeric or alpha sort
if ($sortSize < $realSize) {
//pad with something that should put the entry at the end whether it's number or alpha
$pad = "zzzzzzzz";
//loop through the array and pad the empty values. We can't use array_pad because
//we will lose our key order
for ($row=0;$row<$realSize;$row++) if (!$arr[$row]) $arr[$row] = $pad;
}
//get all the keys from our sorted array. We need to loop by these to maintain
//index association with the rest of the array
$sortArray = array_keys($arr);
$fieldCount = count($fields);
//resort our original array. Map the new field->key to it's original index
foreach ($sortArray AS $key) {
for ($i=0;$i<$fieldCount;$i++) {
$field = $fields[$i];
$newArray[$field][] = $modArray[$field][$key];
}
}
return $newArray;
}
/********************************************************
arrayStringSearch:
this function checks all values in an array
to see if they contain a string. The value
does not have to = the string, just contain
the string. Setting icase = to a value, will
make the search case insensitive. This function
returns the key of the first value that matches
the string. If no matches are found, it returns false
********************************************************/
function arrayStringSearch($str,$arr,$icase = null) {
if (!is_array($arr)) return false;
//we use array_keys so this works on associative arrays as well
$keys = array_keys($arr);
$ret = array();
//loop through and search for a matching string
foreach ($keys AS $key) {
if ($icase) $func = "stripos";
else $func = "strpos";
if ($func($arr[$key],$str)!==FALSE) return $key;
}
//if we get to here, return false;
return false;
}
//shrinks an array down by removing all null values in the array
function reduceArray($arr) {
$newArr = array();
$keys = array_keys($arr);
foreach ($keys AS $key) {
if ($arr[$key]!=NULL) {
if (is_numeric($key)) $newArr[] = $arr[$key];
else $newArr[$key] = $arr[$key];
}
}
return $newArr;
}