phpforeachjunaio

3 × Warnings: Foreach


I've a bit trouble with "foreach". The test page displays:

*Warning: Invalid argument supplied for foreach() in /data/web/virtuals/23481/virtual/www/subdom/vsohbrno/poi/junaio/junaio/test.php on line 60*

*Warning: array_multisort() [function.array-multisort]: Argument #1 is expected to be an array or a sort flag in /data/web/virtuals/23481/virtual/www/subdom/vsohbrno/poi/junaio/junaio/test.php on line 64*

*Warning: Invalid argument supplied for foreach() in /data/web/virtuals/23481/virtual/www/subdom/vsohbrno/poi/junaio/junaio/test.php on line 68*

However I make it for Junaio (AR application for smartphone) - and when I tried to run it here (in the app), I had no trouble with it - it's working. But if the warnings will be disappear also in www pages, it would be fine =o)

My source code:

<?php

require_once '../ARELLibrary/arel_xmlhelper.class.php';

if(!empty($_GET['l']))
    $position = explode(",", $_GET['l']);
else
    trigger_error("user position (l) missing. For testing, please provide a 'l' GET parameter with your request. e.g. pois/search/?l=23.34534,11.56734,0");

//create the xml start
ArelXMLHelper::start(NULL, "/arel/index.html");

//start by defining some positions of geo referenced POIs and give those names and thumbnails
$treasureLocations = array(  
    array("48.6045911000,14.7007322000,0", "Tradiční výroba marmelád a povidel ", "Jih Čech > Šumava > Šumava a Pošumaví. Obec: Pohorská Ves."),  
    array("48.6438239000,14.2208661000,0", "Restaurace Czech Specials (Restaurace Lanovka)", "Jih Čech > Šumava > Šumava a Pošumaví. Obec: Lipno nad Vltavou."),  
    array("48.6541914000,14.0354000000,0", "Plavení dřeva, vorařství – Schwarzenberský plavební kanál", "Jih Čech > Šumava > Šumava a Pošumaví. Obec: Přední Výtoň."),  
);

//in the filter_value, the continent parameter will be send from the client, once the continent is filtered
$filter = $_GET['filter_value'];

//display the POIs as defined in the Constructor
$countpoi = 1;
foreach($treasureLocations as $i => $findPOI)
{    
  //Parameters for distance calculate. $Location = GPS coordinates of the POI, $Position = my GPS position. 
  $location = explode(",", $findPOI[0]);

  //Calculate distance beween my position and POI
  $deg2RadNumber = (float)(pi() / 180); 
  $earthRadius= 6371.009; 
  $latitudeDistance=((float)$position[0]-(float)$location[0])*$deg2RadNumber; 
  $longitudeDistance=((float)$position[1]-(float)$location[1])*$deg2RadNumber; 
  $a=pow(sin($latitudeDistance/2.0),2) +  cos((float)$position[0]*$deg2RadNumber) * cos((float)$location[0]*$deg2RadNumber) * pow(sin($longitudeDistance/2.0),2); 
  $c=2.0*atan2(sqrt($a),sqrt(1.0- $a)); 
  $distance=round($earthRadius*$c); 


  //Set distance for displaying objects (in kilometers)
  if($distance < 10)
  {    
    $title = $findPOI[1];   //Title of the POI
    $poi[$countpoi]['attribution'] = $findPOI[2]; //Description of the POI  
    $poi[$countpoi]['distance'] = $distance; // Distance between POI and me 

    //create the POI
    $poi[$countpoi]['poi'] = ArelXMLHelper::createLocationBasedPOI($i, $title, explode(",", $findPOI[0]), "http://vsohbrno.chudst.cz/poi/poi_obrazek.png", "http://vsohbrno.chudst.cz/poi/poi_obrazek.png", $poi[$countpoi]['attribution'], NULL);    
    $countpoi++;
   }
}       

  //Sorting POI (the closest -> the furthest)      
  foreach ($poi as $val)
  {
    $sortarray[] = $val['distance'];
  }
  array_multisort($sortarray,$poi);


  //Write 39 POI to XML file
  foreach ($poi as $POInumber => $POIvalue)
  {
    if($POInumber < 39){
     if(!empty($filter))
     {
        if(strtolower($filter) == strtolower($poi[$POInumber]['attribution']))
        {
            ArelXMLHelper::outputObject($poi[$POInumber]['poi']); 
        }       
    } 
     else
         {ArelXMLHelper::outputObject($poi[$POInumber]['poi']);}
    }     
  } 



//end the output
ArelXMLHelper::end();

?>

Solution

  • If the distance is never smaller than 10 km, $poi will not be initialised, which is what the error means.

    Initialise $poi to be an array and it should remove the warnings:

    $poi = array();