Warning: Invalid argument supplied for foreach() code: function cs_take_location() { list ($location, ) = explode(".",getenv("HTTP_HOST")); $locations = cs_get_locations(false); foreach ($locations['citynames'] as $cityname=>$city) { if ($cityname == $location) { global $xcityid; return $city[0]; } } foreach ($locations['countrynames'] as $countryname=>$countryid) { if ($countryname == $location) { return -$countryid; } } } pls help me to solve this problem
What about cs_get_locations() ? It seems the $locations variable is not array. Try to check this variable using print_r() that is an array.
That code seems like gibberish or pointless calls... But yeah, without seeing what cs_get_locations does, it's hard to weigh in more. -- edit -- gah, I got lost because of the plural -- that code is really weird.
As others have mentioned, check what $locations is actually containing (for example using var_dump) and also read up on foreach so that you understand what it needs as parameters: http://php.net/manual/en/control-structures.foreach.php
Ok, I THINK I'm grasping what you're trying to do there... did a quick rewrite with some error handling and debug output thrown in. function cs_take_location() { $subdomain = substr($_SERVER['HTTP_HOST'],0,strpos('.',$_SERVER['HTTP_HOST'])); if ($locations = cs_get_locations(false)) { // debug output of $locations if set echo ' <hr /> <h2>$locations debug output</h2> <pre>',print_r($locations),'</pre> <hr />'; return ( isset($locations['citynames'][$subdomain]) ? $locations['citynames'][$subdomain][0] : ( isset($locations['countrynames'][$subdomain']) ? $locations['countrynames'][$subdomain'] : false ) ); } else die('cs_get_locations failed'); } Code (markup): See what that does. -- edit -- I missed that you were looking for the keys, in which case just do a isset on the array for that key... far, FAR simpler.