false,2 => false,3 => false]; $cups[$tmpStr[0]] = true; $max = $tmpStr[1]; for($i = 1;$i <= $max;$i++){ $s = explode(" ", $tmpStr[$i + 1]); $tmp = $cups[$s[0]]; $cups[$s[0]] = $cups[$s[1]]; $cups[$s[1]] = $tmp; } foreach($cups as $key => $value){ if($value === true){ echo $key; } }