You are looping over an EntityCollection, not a regular array or stdclass. So you need to use a proper method getId() or get('someAttribute')