SQL was built to do this type of operation. It's generally a better pattern to fetch the data you need, rather than fetch all the data and then to transform it into what you need in the code.
Edit: upon further reflection I think my application is at the tipping point where it needs another database table.