Turn an array of keys into a hierarchy of objects