Domanda

Ok, ecco il mio dilemma.Sto cercando un modo per consolidare i dati in gruppo utilizzando SimpleXML in PHP.Ecco cosa voglio dire.

Diciamo che ho un xml che assomiglia a questo:

  <favorites>
    <interesting>
        <type>Movie</type>
        <character>James Bond</character>
        <name>Casino Royale</name>
    </interesting>
    <interesting>
        <type>Movie</type>
        <character>Jason Bourne</character>
        <name>Bourne Identity</name>
    </interesting>
    <interesting>
        <type>Book</type>
        <character>Lindsay Ford</character>
        <name>Shantaram</name>
    </interesting>
  </favorites>

Ora qui è quello che voglio che per apparire come:

Film

  • Casino Royale - James Bond Bourne
  • Bourne Identity - Jason Bourne

Prenota

  • Shantaram - Lindsay Ford

Please help me!!Fammi sapere se tutto è confuso.

È stato utile?

Soluzione

Se non si conosce in anticipo il tipo di film, si potrebbe :

  • estrarre tutti i tipi di dati XML
    • ottenere come unico, come possono apparire più volte (Ci potrebbe essere un modo per farlo con XPath ;Io non lo so, quindi non posso utilizzare array_unique)
  • iterare su questi tipi, utilizzando quello che @Mark proposto.


Qualcosa di simile a questo sarebbe probabilmente fare :

$xml = simplexml_load_string($xmlData);

$typesListXml = $xml->xpath('interesting/type');
var_dump($typesListXml);    // var_dump #1

if (!empty($typesListXml)) {
    $typesList = array();
    foreach ($typesListXml as $typeXml) {
        $typesList[] = (string)$typeXml;
    }
    var_dump($typesList);    // var_dump #2

    $typesList = array_unique($typesList);
    var_dump($typesList); // var_dump #3

    $moviesForType = array();
    foreach ($typesList as $type) {
        $rawData = $xml->xpath('interesting[type="' . $type . '"]');
        if (!empty($rawData)) {
            foreach ($rawData as $rawMovie) {
                $moviesForType[$type][] = $rawMovie->name . ' - ' . $rawMovie->character;
            }
        }
    }

    var_dump($moviesForType); // var_dump #4

    // Up to you to present $moviesForType the way you want ;-)

}


E per rendere le cose più facili da capire, ecco il var_dump uscite :

var_dump #1 :

array
  0 => 
    object(SimpleXMLElement)[2]
      string 'Movie' (length=5)
  1 => 
    object(SimpleXMLElement)[3]
      string 'Movie' (length=5)
  2 => 
    object(SimpleXMLElement)[4]
      string 'Book' (length=4)

var_dump #2 :

array
  0 => string 'Movie' (length=5)
  1 => string 'Movie' (length=5)
  2 => string 'Book' (length=4)

var_dump #3 :

array
  0 => string 'Movie' (length=5)
  2 => string 'Book' (length=4)

var_dump #3 :

array
  'Movie' => 
    array
      0 => string 'Casino Royale - James Bond' (length=26)
      1 => string 'Bourne Identity - Jason Bourne' (length=30)
  'Book' => 
    array
      0 => string 'Shantaram - Lindsay Ford' (length=24)


Ora, spetta a voi per presentare questi dati nel modo desiderato ;un doppio ciclo foreach costruzione <ul> e <li> tag probabilmente farebbe ;-)

Altri suggerimenti

Io credo che si sta cercando di Xpath:

$xmlData = '<favorites>...</favorites>';
$xml = simplexml_load_string($xmlData);
$arrMovies = $xml->xpath('interesting[type="Movie"]');
$arrBooks = $xml->xpath('interesting[type="Book"]');

È quindi possibile eseguire un ciclo $arrMovies e $arrBooks e stampare quello che ti serve:

foreach($arrMovies as $movie) {
    echo $movie->name;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top