Just can't figure out why implode doesn't work as expected.

Hello guys, I devised the code block below, to retrieve all US zip codes that are located within a specified radius from the user’s own zip code. So basically, the user selects a maximum distance option from a form element (not shown here)which is appended to the variable $distance. What the code below does is as follows:
The longitude and latitude corresponding to the user’s zip code are retrieved in the first query. Then in the second query, the longitudes and latitudes corresponding to every zip code in the database are retrieved. In the ensuing while loop, a distance calculation is made between the user’s zip and every other zip using the longitudes and latitudes and if that distance falls within the selected $distance, that particular zip code is listed in a defined array called $range. Now everything works fine up to this point as tested. The problem is with the very last line. I try to implode the $range array into a string with the same name, separating the array elements with commas (,). Then when I print out the resulting string, I get the list of desired zip codes but not separated by commas. For example:

900119001590017900349003790043900479004890056900619006290301 9030190301 90302

Well when I use a foreach loop as follows:

foreach ($range as $r) {echo $r.",";} the $range array behaves like any normal array yielding:

90011,90015,90017,90034,90037,90043,90047,90048,90056,90061,90062,90301 ,90301,90301 ,90302,

So why in the world is the implode function not working? Here is my code.

[php]
//Retrieve the zip codes within range.
// Connect to the database.
require(‘config.php’);

//Retrieve longitude and latitude of logged in member.

$query = “SELECT* FROM members INNER JOIN zip_codes ON members.zip = zip_codes.zip WHERE members.member_id = ‘{$_SESSION[‘id’]}’”;

$result = mysql_query($query);

$row = mysql_fetch_assoc($result);

$my_lon = $row[‘lon’];

$my_lat = $row[‘lat’];

//Query all longitudes and latitudes in database.

$query2 = "SELECT* FROM zip_codes INNER JOIN members ON members.zip = zip_codes.zip ";

$result2 = mysql_query($query2);

while ($row2 = mysql_fetch_assoc($result2)) {

//Define array to hold zips found within range.

$range = array();  

if((rad2deg(acos(sin(deg2rad($my_lat))*sin(deg2rad($row2['lat'])) +cos (deg2rad($my_lat)) * cos (deg2rad($row2['lat'])) * cos(deg2rad($my_lon - $row2['lon']))   ) ) )*69.09 <= $distance )

 { $range[] = $row2['zip']; } 

//Implode the range arrary.

$range = implode(',' , $range);   echo $range;

}//End of while loop.
[/php]
[

Implode is working, but your $range array always contains just 1 element. See, you need to move this line outside your while loop (put it above the while loop):
[php]$range = array();[/php]

Oh, and this row too (put it after the while loop closing } ):
[php]$range = implode(’,’ , $range); echo $range;[/php]

@ Admin

When I take the $range = implode(’,’ , $range); echo $range; line outside the loop as u suggested, nothing gets printed. So I guess the array is only defined within the loop. :frowning:

As for the suggestion about moving the $range = array(); line out of the loop, it seems as if PHP doesn’t recognize the $range as an array while within the loop becasue when I run the results based on that suggestion, I get an error message saying that the [] operator is not recognized for strings.

This is how exactly I suggested to change your code:
[php]//Define array to hold zips found within range.

$range = array();

while ($row2 = mysql_fetch_assoc($result2)) {

if((rad2deg(acos(sin(deg2rad($my_lat))*sin(deg2rad($row2[‘lat’])) +cos (deg2rad($my_lat)) * cos (deg2rad($row2[‘lat’])) * cos(deg2rad($my_lon - $row2[‘lon’])) ) ) )*69.09 <= $distance )
{ $range[] = $row2[‘zip’]; }

}//End of while loop.

//Implode the range arrary.

$range = implode(’,’ , $range); echo $range;[/php]

Hope you do understand that having $range = array(); WITHIN the while loop will just re-set your array to blank on each iteration, so it will never contain more than one element.

Yes, if you have $range = array() inside the while loop, it will reset the array every time it starts over.

taking both lines out of the loop makes the code work :slight_smile: thanks all for ur contributions.

Sponsor our Newsletter | Privacy Policy | Terms of Service