fetch(PDO::FETCH_ASSOC)) { $tmp[(int)$row['locationid']][$row['setting']] = $row['value']; } // $matchingLocations contains the location ids sorted from closest to furthest, so we use it to make sure the order // in which they override is correct (closest setting wins, e.g. room setting beats department setting) foreach ($matchingLocations as $lid) { if (!isset($tmp[$lid])) continue; foreach ($tmp[$lid] as $setting => $value) { if (!isset($configVars[$setting])) { $configVars[$setting] = $value; } } } unset($tmp); }