Getting foreach() error

Discussion in 'PHP' started by sheril123, May 3, 2013.

  1. #1
    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
     
    sheril123, May 3, 2013 IP
  2. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #2
    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.
     
    Hamidsam, May 3, 2013 IP
    ryan_uk likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    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.
     
    deathshadow, May 3, 2013 IP
  4. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #4

    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
     
    ryan_uk, May 3, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    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.
     
    Last edited: May 3, 2013
    deathshadow, May 3, 2013 IP