Use foreach_set() instead of reimplementing it.