samedi 1 août 2015

Parsing xml with php (create multidimensional array with for loop on elements of XML)

i have a file.xml in folder... This file is:

<xml>
 <alert>
  <info>
   <area>
    <circle>18.980,153.480 0</circle>
   <area>
  </info>
  <info>
   <area>
    <circle>28.980,163.480 0</circle>
   <area>
  </info>
  <info>
   <area>
    <circle>38.980,173.480 0</circle>
   <area>
  </info>
 </alert>

i have an empty array and i want to creare a multidimensional array with numbers in element circle, so i must do a for loop for , but in my file xml i don't know how many info there are.

$url = "outvulcani/report.xml";
$xml = simplexml_load_file($url);

$numinfo = substr_count($xml, "<info>");

for ($i = 0; $i <= $numinfo; $i++) {


$entry = $xml->alert->info[$i];
$coordinate = $entry->area->cirle;
$virgola = strpos($coordinate,",");
$spazio = strpos($coordinate," ");
$virg = $virgola;
$virgLng = $virgola+1;
$spaz = $spazio-$virgola;   
$lat = substr($coordinate,0,$virg);
$lng = substr($coordinate,$virgLng,$spaz);
$report[] = array($lat,$lng);

}

at the end i would like that the array $report is:

Array
(
      [0] => Array
        (
            [0] => 18.980
            [1] => 153.480 
        )

     [1] => Array
        (
            [0] => 28.980
            [1] => 163.480 
        )

     [2] => Array
       (
        [0] => 38.980
        [1] => 173.480 
      )

 )

sorry for my english

Aucun commentaire:

Enregistrer un commentaire