Fix invalid argument supplied for foreach() error