I am writing this code, but instead of executing it in the browser, such a thing appears. The site redirected too many times, tell me how to fix it?

function redirect_subdomen( $current_city, $arr ) { foreach($arr as $k => $v){ if($k == $current_city){ header("Location: http://".$v[0]);break; } } } if($arAuto["CITY_NAME"] == $usrSelCity){ redirect_subdomen($arAuto["CITY_NAME"], $arrDate); } if($arAuto["CITY_NAME"] != $usrSelCity){ redirect_subdomen($usrSelCity, $arrDate); } 
  • For a start, the question is what is the meaning of the two conditions at the end of the code? Both in that and in that case the same code is executed, isn't it? - Stanislav Belichenko
  • I corrected my code, I don’t understand why there is a looping, despite the fact that I put a break on coincidence - ChromeChrome
  • one
    You can fix it even better, do not two if , but if {} else {} . In general, look at your code: header("Location: http://".$v[0]) - you always go to the address that is contained in the array element with index 0. The correct code will be like this: header("Location: http://".$v[$k]) . If I am right, let me know, I will issue this comment as an answer to your question, then you can accept it. - Stanislav Belichenko
  • one
    Indeed, the structure of your array should be indicated in the question itself. In this case, the following code will suit you: header("Location: http://".$v[$k][0]) - Stanislav Belichenko
  • one
    @ Stanislav is either $arr[$k][0] or $v[0] (as it was). and your $v[$k][0] is some kind of nonsense. - teran

1 answer 1

Solved the problem as follows

 if($_SESSION["CUSTON_CITY"] == NULL){ foreach($arrDate as $k => $v){ if($k == $arAuto["CITY_NAME"]){ $_SESSION["CUSTON_CITY"] = "1"; header("Location: http://".$v[0]); break; } } } 
  • Only probably CUSTOM;) - Stanislav Belichenko