Greetings!
Given this query:

<?php
$me = array()
$query = array('lat' => 59.95, 'lng' => 10.77);
$result = geonames_query('countrycode', $query);
foreach ($result->results as $place) {
   $me[] = $place[countryname];
}
?>

How do I ensure that this snippet does not return an empty result set? When no result, foreach gives warnings saying array does not exist. In this case that happens over oceans and international land where there is no country.
Tried using if (!empty($result->results)){foreach_query} else {$me = "international"};, though it did not work.

Thanks for all feedback! :)

Comments

2xe’s picture

Status: Active » Closed (fixed)

just add a test;

if (!empty($result->results)) {
  foreach ($result->results as $place) {
     $me[] = $place[countryname];
  }
}