1

I am extracting XML from a feed. There is a tag entitled:

<georss:point>34.234 -34.435</georss:point> 

which contains two variables I am inserting into MySQL When I run this code, the variable is 'array'. I then place an extract to break out the variables, unsure of next

$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));
    array(
            'lat'=>$latlng[0],
            'lng'=>$latlng[1]
    );

    $lat = array($latlng[0]);
    $lng = array($latlng[1]);

    echo $lat;
    echo $lng;  
} 

When I place those echo statements (in the last two lines of code), the variables get echod to the screen. However when I place these varaibles outside of the array, the values do not get echod. I am attempting to get these variables outside of the array, so that I can insert these variables into the database. I have tried extract on the varaiables, but this prints back to the screen as 'Array'... unsure of what I need to do to extract these variables from the array. Thanks,

1
  • is there only one lat and long, or many? Commented Aug 15, 2011 at 18:52

4 Answers 4

1

Replace your foreach with a version like this...

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

var_dump($new_array);

this should give you a array of your lat/long values.

Sign up to request clarification or add additional context in comments.

Comments

0

Some observations.

1) you can register xpath namespaces instead of replacing in the string
2) array('lat'=>$latlng[0],'lng'=>$latlng[1]); does nothing. It is a dead store.
3) Instead of

$lat = array($latlng[0]);
$lng = array($latlng[1]);
echo $lat;
echo $lng;  

You can do: echo $latlng[0].'-'.$latlng[1];
If you try to echo an array, you will always get Array.
Use a specific index to echo an individual element.

4) Alternatively you could use list to store the variables directly
list($lat, $long) = explode(' ',trim($item->point));

Edit To insert multiple results into your database, store the results in an array:

$lat_long = array();
foreach($items as $item){
    $lat_long[] = explode(' ', $item);
}

Then later iterate through this array and do your database insert.

foreach($lat_long as $point){   
  //do SQL injection prevention as well
  //INSERT INTO your_table (`lat`, `long`) VALUES ($point[0], $point[1]);
}

2 Comments

Hi Brian, Your suggestion makes more sense to me... however when I implement, the values are the same. list($lat, $long) = explode(' ',trim($item->point)); the lat and lng are always the same on all 23 items.
Ahhh... 'I see' said the blind man! Thank you very much Brian!
0

I think this:

$lat = array($latlng[0]);
$lng = array($latlng[1]);

should be:

$lat = $latlng[0];
$lng = $latlng[1];

Comments

0

Obmitting the array(..) around $latlng[..] would do the trick, I think.

$lat = $latlng[0];
$lng = $latlng[1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.