PHP - Extract XML data to a 2d array -


I was thinking that someone could give me a hand.

I have a KML file (Google Maps XML) and I need to remove the coordinates in a 2D array.

The format of the file is as follows:

  & lt ;? Xml version = "1.0" encoding = "UTF-8" & gt; & Lt; Kml xmlns = "http://earth.google.com/kml/2.1" & gt; & Lt; Document & gt; & Lt; Placemark & ​​gt; & Lt; Name & gt; Im a name & lt; / Name & gt; & Lt; Point & gt; & Lt; Coordinate & gt; 138.611798, -34.9 26053 & lt; / Coordinate & gt; & Lt; / Point & gt; & Lt; / Placemark & ​​gt; & Lt; Placemark & ​​gt; & Lt; Name & gt; Im a name & lt; / Name & gt; & Lt; Point & gt; & Lt; Coordinate & gt; 138.611798, -34.9 26053 & lt; / Coordinate & gt; & Lt; / Point & gt; & Lt; / Placemark & ​​gt; & Lt; Placemark & ​​gt; & Lt; Name & gt; Im a name & lt; / Name & gt; & Lt; Point & gt; & Lt; Coordinate & gt; 138.611798, -34.9 26053 & lt; / Coordinate & gt; & Lt; / Point & gt; & Lt; / Placemark & ​​gt; [...]  

I should be able to return an array with the following format:

  Array ([0] => Array [0] = & gt; 138.611798 [1] => -34.926053) [1] = & gt; Hey ([0] => 138.611798 [1] => -34.926053)  

There will be a lot of digits in my KML file, so I'm able to do it automatically needed.

Any help will really be appreciated.

Thanks

You can use SimpleXML only to get the desired value And can push them inside the array. Example:

  $ Data = array (); $ Xml = simplexml_load_file ('path / to / file.kml'); Foreach ($ xml- & gt; Document-> Placemark as $ placemark) {list ($ x, $ y) = explosion (',', $ placemark-> point- & gt; coordinate ); // coordinate explosion by comma $ data [] = array ($ x, $ y);} echo '& lt; Pre & gt; '; Print_r ($ data);  


Comments