Hi,
If you don't know in advance the types of movies, you could :
- extract all types from the XML data
- get them as unique, as they may appear several times (There might be a way to do that with XPath ; I don't know it, so I'll use
array_unique
)
- iterate over these types, using what @Mark proposed.
Something like this would probably do :
$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 ;-)
}
And to make things easier to understand, here are the var_dump's outputs :
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)
Now, it's up to you to present those data the way you want ; a double-foreach loop constructing <ul>
and <li>
tags would probably do ;-)