Refactor canonicalizing array indices to a helper function

No functionality changes.