Only iterate through result set once

Our current code iterated through the result set twice, by calling
iterator_to_array before using array_map. Using a simple foreach loop
only requires going through the result set once.

Change-Id: I36b93268a3390c07bb7f685faef9cc3cadd254e8