DBHelper::reIndex PHP Method

reIndex() public static method

* reIndex For an array of assoc rays, return a new array of assoc rays using a certain field for keys
public static reIndex ( )
    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];
            }
            $target = $obj;
        }
        return $R;
    }

Usage Example

Example #1
0
 function test_4_null()
 {
     DB::query("UPDATE accounts SET password = NULL WHERE username=%s", 'Bart');
     $all = DB::query("SELECT * FROM accounts ORDER BY id ASC");
     $ages = DBHelper::verticalSlice($all, 'age', 'password');
     $this->assert(count($ages) === 5);
     $this->assert($ages[''] === '15');
     $passwords = DBHelper::reIndex($all, 'password');
     $this->assert(count($passwords) === 5);
     $this->assert($passwords['']['username'] === 'Bart');
     $this->assert($passwords['']['password'] === NULL);
 }