Hallo,
ich hänge nun schon länger an einem Problem und hoffe, dass mir hier jemand helfen kann. Ich weiß, dass es hier ähnliche Einträge gibt, aber ich habs nicht geschafft, das auf mein Problem zu übertragen. Ich bin noch ein PHP-Anfänger.
Ich habe ein mehrdimensionales Array, das ich gerne als entsprechende Liste (<ul>) darstellen möchte.
PHP-Code:
SimpleXMLElement Object
(
[CategoryType] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => genre
)
[Category] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rating] => -1
[id] => 7318639
[name] => Blues
[videoCount] => 0
[artistCount] => 134
[releaseCount] => 2098
[trackCount] => 27200
[hasVideoStation] => 0
[hasAudioStation] => 0
)
[Category] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rating] => -1
[id] => 7318815
[name] => Acoustic Blues
[videoCount] => 0
[artistCount] => 34
[releaseCount] => 611
[trackCount] => 8072
[hasVideoStation] => 0
[hasAudioStation] => 0
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rating] => -1
[id] => 7318681
[name] => Blues-Rock
[videoCount] => 0
[artistCount] => 43
[releaseCount] => 700
[trackCount] => 8311
[hasVideoStation] => 0
[hasAudioStation] => 0
)
)
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rating] => -1
[id] => 7318827
[name] => Comedy
[videoCount] => 0
[artistCount] => 299
[releaseCount] => 496
[trackCount] => 6849
[hasVideoStation] => 0
[hasAudioStation] => 0
)
[Category] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[rating] => -1
[id] => 7318828
[name] => Country Comedy
[videoCount] => 0
[artistCount] => 27
[releaseCount] => 190
[trackCount] => 2177
[hasVideoStation] => 0
[hasAudioStation] => 0
)
)
Ich will nun so eine Liste ausgeben mit Genrenamen und ihren jeweiligen Untergenres:
- Blues
Acoustic Blues
Blues-Rock
- Comedy
Country Comedy
Das funktioniert soweit auch, nur bekomme ich immer zu viele bzw. nicht geschlossene <ul>-Tags. Ich habe nun einiges über Rekursion und is_array gelesen und versucht, aber irgendwie bekomme ich es einfach nicht hin. Hier ist mal der Code, der die Liste ausgibt:
PHP-Code:
echo "<ul>";
foreach ($genre as $kategorie) {
foreach ($kategorie as $kat) {
echo "<li>".$kat->attributes()->name."</li>";
foreach ($kat as $variable) {
if ($variable->attributes()->name != '' ){
echo "<ul>";}
echo "<li>".$variable->attributes()->name.</li></ul>";
}
}echo "</ul>";
}
Ich hoffe das ist verständlich beschrieben.
Ich würde mich sehr freuen, wenn mir jemand helfen kann.
Danke schon mal,
Kristina