verticalslice and reindex cleanups and tests
This commit is contained in:
15
db.class.php
15
db.class.php
@@ -882,10 +882,18 @@ class DBHelper {
|
||||
*/
|
||||
|
||||
public static function verticalSlice($array, $field, $keyfield = null) {
|
||||
$array = (array) $array;
|
||||
|
||||
$R = array();
|
||||
foreach ($array as $obj) {
|
||||
if ($keyfield) $R[$obj[$keyfield]] = $obj[$field];
|
||||
else $R[] = $obj[$field];
|
||||
if (! array_key_exists($field, $obj)) die("verticalSlice: array doesn't have requested field\n");
|
||||
|
||||
if ($keyfield) {
|
||||
if (! array_key_exists($keyfield, $obj)) die("verticalSlice: array doesn't have requested field\n");
|
||||
$R[$obj[$keyfield]] = $obj[$field];
|
||||
} else {
|
||||
$R[] = $obj[$field];
|
||||
}
|
||||
}
|
||||
return $R;
|
||||
}
|
||||
@@ -898,12 +906,15 @@ class DBHelper {
|
||||
public static function reIndex() {
|
||||
$fields = func_get_args();
|
||||
$array = array_shift($fields);
|
||||
$array = (array) $array;
|
||||
|
||||
$R = array();
|
||||
foreach ($array as $obj) {
|
||||
$target =& $R;
|
||||
|
||||
foreach ($fields as $field) {
|
||||
if (! array_key_exists($field, $obj)) die("reIndex: array doesn't have requested field\n");
|
||||
|
||||
$nextkey = $obj[$field];
|
||||
$target =& $target[$nextkey];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user