Using array functions instead of foreach to generate output