From 24c1b930dfef3a157eb19fc7d662ce342c21e7c7 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Tue, 18 Sep 2012 20:13:43 -0700 Subject: [PATCH] add DBHelper class with functions verticalSlice and reIndex --- db.class.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/db.class.php b/db.class.php index 290de68..8afdaf4 100644 --- a/db.class.php +++ b/db.class.php @@ -867,6 +867,45 @@ class MeekroDBException extends Exception { public function getQuery() { return $this->query; } } +class DBHelper { + /* + verticalSlice + 1. For an array of assoc rays, return an array of values for a particular key + 2. if $keyfield is given, same as above but use that hash key as the key in new array + */ + + public static function verticalSlice($array, $field, $keyfield = null) { + $R = array(); + foreach ($array as $obj) { + if ($keyfield) $R[$obj[$keyfield]] = $obj[$field]; + else $R[] = $obj[$field]; + } + return $R; + } + + /* + reIndex + For an array of assoc rays, return a new array of assoc rays using a certain field for keys + */ + + public static function reIndex() { + $fields = func_get_args(); + $array = array_shift($fields); + + $R = array(); + foreach ($array as $obj) { + $target =& $R; + + foreach ($fields as $field) { + $nextkey = $obj[$field]; + $target =& $target[$nextkey]; + } + $target = $obj; + } + return $R; + } +} + function meekrodb_error_handler($params) { if (isset($params['query'])) $out[] = "QUERY: " . $params['query']; if (isset($params['error'])) $out[] = "ERROR: " . $params['error'];