The $nearbyevents array you define at the top and the one you reference inside your function are different variables with the same name.
If you want your function to refer to a variable you have defined in the global scope, you can declare it as global, which is generally discouraged:
function getnearby() {
global $nearbyevents;
include("includes/db.php");
$query="SELECT * from `events` order by `id` asc";
$result=$db->query($query);
for ($i=0;$i<$result->num_rows;$i++){
$row=$result->fetch_assoc();
$nearbyevents[]=array('title'=>$row['event_title'],'date'=>$row['event_date'],'city'=>$row['event_city'],'details'=>$row['event_details'],'url'=>$row['event_url']);
}
}
You could also pass the parameter by reference into the function:
$global_nearbyevents=array(); // i changed this var's name to distinguish it from the other var
function getnearby(&$nearbyevents) { // NOTE the ampersand!!!
include("includes/db.php");
$query="SELECT * from `events` order by `id` asc";
$result=$db->query($query);
for ($i=0;$i<$result->num_rows;$i++){
$row=$result->fetch_assoc();
$nearbyevents[]=array('title'=>$row['event_title'],'date'=>$row['event_date'],'city'=>$row['event_city'],'details'=>$row['event_details'],'url'=>$row['event_url']);
}
}
getnearby($global_nearbyevents);
You could also design your function to return an array:
function getnearby() { // NOTE the ampersand!!!
$nearbyevents = array();
include("includes/db.php");
$query="SELECT * from `events` order by `id` asc";
$result=$db->query($query);
for ($i=0;$i<$result->num_rows;$i++){
$row=$result->fetch_assoc();
$nearbyevents[]=array('title'=>$row['event_title'],'date'=>$row['event_date'],'city'=>$row['event_city'],'details'=>$row['event_details'],'url'=>$row['event_url']);
}
return $nearbyevents;
}
$global_nearbyevents = getnearby();