Change while for foreach loop