Note that there are some explanatory texts on larger screens.

plurals
  1. PONotice: Undefined offset using foreach with xml
    primarykey
    data
    text
    <p>I've been searching through previous questions for the past hour, so if this exists somewhere else, sorry, but I didn't find it. I'm pretty new to PHP, so forgive me if it's a newbie question. I searched through Google, too.</p> <p>I'm trying to automate getting the lat/long information from airnav.com for various airports for map updates. I've got an XML file with the airport codes:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;AIRPORTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;AIRPORT&gt;MXA&lt;/AIRPORT&gt; &lt;AIRPORT&gt;SDM&lt;/AIRPORT&gt; &lt;/AIRPORTS&gt; </code></pre> <p>My PHP is:</p> <pre><code>&lt;?php $xml=simplexml_load_file("airports.xml"); foreach($xml-&gt;AIRPORT as $arpt) { if (preg_match('#[0-9]#', $arpt)){ $arptName = $arpt;} else{ $arptName = "K" . $arpt;} $content = file_get_contents("http://www.airnav.com/airport/" . $arptName); preg_match('#([0-9]{2,2}\.[0-9]{1,})\s\/\s(\-[0-9]{2,2}\.[0-9]{1,})#', $content, $arptmatch); $lat = $arptmatch[1]; //ERROR HERE (line 27) $long = $arptmatch[2]; //AND HERE (line 28) echo "Airport: $arpt\nLat: $lat\nLong: $long\n"; } ?&gt; </code></pre> <p>This works just fine for the first airport, I get this:</p> <pre><code>Airport: MXA Lat: 35.8944444 Long: -90.1545833 </code></pre> <p>Which, when I go to the site and check is correct. But for the second airport, I get:</p> <pre><code>Airport: SDM Lat: Long: </code></pre> <p>Along with the following errors:</p> <pre><code>Notice: Undefined offset: 1 in /home/a5308473/public_html/phpStuff/readFromAirNav.html on line 27 Notice: Undefined offset: 2 in /home/a5308473/public_html/phpStuff/readFromAirNav.html on line 28 </code></pre> <p>I can't seem to find any way to fix this. Anyone know what's wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload