dongliao2241 2014-01-01 13:45
浏览 25
已采纳

3×警告: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();

?>
  • 写回答

1条回答 默认 最新

  • dongshanni1611 2014-01-01 14:00
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像