2016-04-10 12 views
0

Ich bin ein totaler Anfänger PHP und brauchen Haben Sie etwas Hilfe :-)Wie zu verschachtelten Arrays xml zu php?

dieses xml:

http://pastebin.com/ZSpNPhXH

Und diese Scrypt:

<?php 

// Retrieve XML File 
$file = file_get_contents('livescore-feed.xml'); 

// Parse XML with SimpleXML 
$livescore_data = new SimpleXMLElement($file); 


foreach ($livescore_data->league as $league) { 

    echo "<table class='table table-striped table-bordered table-condensed'>"; 


    echo "<thead><tr><th colspan='6' id='league'>" . $league->attributes()->name . "</th></tr></thead>"; 

    echo "<tbody>"; 
    foreach ($league->match as $match) { 




     $status = $match->attributes()->status; 
     $home = $match->home->attributes()->name; 
     $away = $match->away->attributes()->name; 
     $score = $match->home->attributes()->goals . " - " . $match->away->attributes()->goals; 

     // If match not yet started, there will be a ":" in 'status' attribute 
     if (strpos($match->attributes()->status,':') !== false) { 
      $score = "-"; 
     } 



     echo "<tr><td class='status' id='match'>" . $status . "</td><td id='match' colspan='2'>" . 
      $home . "</td><td class='score' id='match'>" . $score . "</td><td id='match' colspan='2'>" . 
      $away . "</td> 


       </tr>"; 

    } 


    echo "</tbody></table>"; 
} 

>

Wie um "Ereignisse" auch anzuzeigen/zu analysieren

Vielen Dank für Ihre Hilfe und Vorschläge

Antwort

0

Auf die gleiche Weise, die Sie andere Daten analysieren!

foreach($livescore_data->league as $league) 
{ 
    (...) 
    foreach($league->match as $match) 
    { 
     (...) 
     #  ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 
     foreach($match->events->event as $event) 
     { 
      echo $event['assist'] . '<br>'; 
      echo $event['player'] . '<br>'; 
      (...) 
     } 
    } 
} 

Sie haben eine Schleife durch $match->events->event ausführen und Sie können jedes <event> Attribut $event[attribute] Syntax erhalten. So, $event['extra_min'], $event['minute'], etc ...

Sie müssen nur den Code an Ihre Tabellenstruktur anpassen.