开发者

PHP Namespace XML Array Issue

开发者 https://www.devze.com 2023-03-27 21:58 出处:网络
Hello I am using 开发者_如何学Cthe NOAA buoy RSS feed to create and insert variables into Mysql. There is a tag entitled:

Hello I am using 开发者_如何学Cthe NOAA buoy RSS feed to create and insert variables into Mysql. There is a tag entitled:

<georss:point>45.565 -34.123</georss:point>

Which I have broken out as such:

$xmlString = file_get_contents($feed_url); 
$xmlString = str_replace('georss:point','point',$xmlString);  
$xml = new SimpleXMLElement($xmlString); 
$items = $xml->xpath('channel/item'); 
$closeItems = array(); foreach($items as $item) 
{     
$latlng = explode(' ',trim($item->point));
$lat = $latlng[0];
$lng = $latlng[1];  
} 

However the issue that I am running into, the variables for $lat and $lng are the same for all the feed articles. Each article will have a unique $lat and $lng, so I know I am doing something wrong here. Thanks Again,


The problem is here:

foreach($items as $item) 
{     
    $latlng = explode(' ',trim($item->point));
    $lat = $latlng[0];
    $lng = $latlng[1];  
}

You assign the variables for each record, but never use them inside the loop. Assuming you have more code after the loop, that code will always receive only the last values assigned for $lat and $lng.

To correct the problem, place your record-specific code within the foreach() loop:

foreach($items as $item) 
{     
    $latlng = explode(' ',trim($item->point));
    $lat = $latlng[0];
    $lng = $latlng[1];
    // Do something with $lat and $lng here  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消