I get this XML:
<?xml version="1.0"?>
<Assets xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
<Asset Title="Star Wars: The Rise of Skywalker" Description="Trailer 3" DateAdded="2019-10-22" DateModified="2019-10-22" TheatricalReleaseDate="2019-12-20" Studio="Walt Disney Studios Motion Pictures" Year="2019" EClipId="e166012" TrailerTypeId="2" TrailerVersion="1" LanguageName="English" LanguageId="52" MpaaRating="Not Rated" MpaaRatingId="34" Runtime="149" RatingId="30" ReleaseType="2" BitrateId="494" FormatId="20" TitleId="133393" ReleaseId="173863" CountryId="222" Country="United States" Director="J.J. Abrams" GenreIds="101,102,110" Genre="Action,Adventure,Fantasy">
<Identifiers Name="IMDB" Value="tt2527338" />
<Url>https://www.fileurl.com</Url>
</Asset>
<Asset Title="Star Wars: The Rise of Skywalker" Description="Trailer 2" DateAdded="2019-08-26" DateModified="2019-08-26" TheatricalReleaseDate="2019-12-20" Studio="Walt Disney Studios Motion Pictures" Year="2019" EClipId="e164936" TrailerTypeId="2" TrailerVersion="1" LanguageName="English" LanguageId="52" MpaaRating="Not Rated" MpaaRatingId="34" Runtime="131" RatingId="34" ReleaseType="2" BitrateId="494" FormatId="20" TitleId="133393" ReleaseId="173863" CountryId="222" Country="United States" Director="J.J. Abrams" GenreIds="101,102,110" Genre="Action,Adventure,Fantasy">
<Identifiers Name="IMDB" Value="tt2527338" />
<Url>https://www.fileurl.com</Url>
</Asset>
<Asset Title="Star Wars: The Rise of Skywalker" Description="Trailer 1" DateAdded="2019-04-12" DateModified="2019-04-29" TheatricalReleaseDate="2019-12-20" Studio="Walt Disney Studios Motion Pictures" Year="2019" EClipId="e163048" TrailerTypeId="2" TrailerVersion="1" LanguageName="English" LanguageId="52" MpaaRating="Not Rated" MpaaRatingId="34" Runtime="130" RatingId="30" ReleaseType="2" BitrateId="494" FormatId="20" TitleId="133393" ReleaseId="173863" CountryId="222" Country="United States" Director="J.J. Abrams" GenreIds="101,102,110" Genre="Action,Adventure,Fantasy">
<Identifiers Name="IMDB" Value="tt2527338" />
<Url>https://www.fileurl.com</Url>
</Asset>
</Assets>
I get this by curl, and then do:
$xml_object = simplexml_load_string($string);
$json = json_encode( $xml_object );
$xml_array = json_decode( $json, true );
var_dump($xml_array);
and this outputs:
array(1) {
["Asset"]=>
array(3) {
[0]=>
array(3) {
["@attributes"]=>
array(26) {
["Title"]=>
string(32) "Star Wars: The Rise of Skywalker"
["Description"]=>
string(9) "Trailer 3"
["DateAdded"]=>
string(10) "2019-10-22"
["DateModified"]=>
string(10) "2019-10-22"
["TheatricalReleaseDate"]=>
string(10) "2019-12-20"
["Studio"]=>
string(35) "Walt Disney Studios Motion Pictures"
["Year"]=>
string(4) "2019"
["EClipId"]=>
string(7) "e166012"
["TrailerTypeId"]=>
string(1) "2"
["TrailerVersion"]=>
string(1) "1"
["LanguageName"]=>
string(7) "English"
["LanguageId"]=>
string(2) "52"
["MpaaRating"]=>
string(9) "Not Rated"
["MpaaRatingId"]=>
string(2) "34"
["Runtime"]=>
string(3) "149"
["RatingId"]=>
string(2) "30"
["ReleaseType"]=>
string(1) "2"
["BitrateId"]=>
string(3) "494"
["FormatId"]=>
string(2) "20"
["TitleId"]=>
string(6) "133393"
["ReleaseId"]=>
string(6) "173863"
["CountryId"]=>
string(3) "222"
["Country"]=>
string(13) "United States"
["Director"]=>
string(11) "J.J. Abrams"
["GenreIds"]=>
string(11) "101,102,110"
["Genre"]=>
string(24) "Action,Adventure,Fantasy"
}
["Identifiers"]=>
array(1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(4) "IMDB"
["Value"]=>
string(9) "tt2527338"
}
}
["Url"]=>
string(81) "https://www.fileurl.com"
}
[1]=>
array(3) {
["@attributes"]=>
array(26) {
["Title"]=>
string(32) "Star Wars: The Rise of Skywalker"
["Description"]=>
string(9) "Trailer 2"
["DateAdded"]=>
string(10) "2019-08-26"
["DateModified"]=>
string(10) "2019-08-26"
["TheatricalReleaseDate"]=>
string(10) "2019-12-20"
["Studio"]=>
string(35) "Walt Disney Studios Motion Pictures"
["Year"]=>
string(4) "2019"
["EClipId"]=>
string(7) "e164936"
["TrailerTypeId"]=>
string(1) "2"
["TrailerVersion"]=>
string(1) "1"
["LanguageName"]=>
string(7) "English"
["LanguageId"]=>
string(2) "52"
["MpaaRating"]=>
string(9) "Not Rated"
["MpaaRatingId"]=>
string(2) "34"
["Runtime"]=>
string(3) "131"
["RatingId"]=>
string(2) "34"
["ReleaseType"]=>
string(1) "2"
["BitrateId"]=>
string(3) "494"
["FormatId"]=>
string(2) "20"
["TitleId"]=>
string(6) "133393"
["ReleaseId"]=>
string(6) "173863"
["CountryId"]=>
string(3) "222"
["Country"]=>
string(13) "United States"
["Director"]=>
string(11) "J.J. Abrams"
["GenreIds"]=>
string(11) "101,102,110"
["Genre"]=>
string(24) "Action,Adventure,Fantasy"
}
["Identifiers"]=>
array(1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(4) "IMDB"
["Value"]=>
string(9) "tt2527338"
}
}
["Url"]=>
string(81) "https://www.fileurl.com""
}
[2]=>
array(3) {
["@attributes"]=>
array(26) {
["Title"]=>
string(32) "Star Wars: The Rise of Skywalker"
["Description"]=>
string(9) "Trailer 1"
["DateAdded"]=>
string(10) "2019-04-12"
["DateModified"]=>
string(10) "2019-04-29"
["TheatricalReleaseDate"]=>
string(10) "2019-12-20"
["Studio"]=>
string(35) "Walt Disney Studios Motion Pictures"
["Year"]=>
string(4) "2019"
["EClipId"]=>
string(7) "e163048"
["TrailerTypeId"]=>
string(1) "2"
["TrailerVersion"]=>
string(1) "1"
["LanguageName"]=>
string(7) "English"
["LanguageId"]=>
string(2) "52"
["MpaaRating"]=>
string(9) "Not Rated"
["MpaaRatingId"]=>
string(2) "34"
["Runtime"]=>
string(3) "130"
["RatingId"]=>
string(2) "30"
["ReleaseType"]=>
string(1) "2"
["BitrateId"]=>
string(3) "494"
["FormatId"]=>
string(2) "20"
["TitleId"]=>
string(6) "133393"
["ReleaseId"]=>
string(6) "173863"
["CountryId"]=>
string(3) "222"
["Country"]=>
string(13) "United States"
["Director"]=>
string(11) "J.J. Abrams"
["GenreIds"]=>
string(11) "101,102,110"
["Genre"]=>
string(24) "Action,Adventure,Fantasy"
}
["Identifiers"]=>
array(1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(4) "IMDB"
["Value"]=>
string(9) "tt2527338"
}
}
["Url"]=>
string(81) "https://www.fileurl.com"
}
}
}
I then run a loop:
foreach($xml_array['Asset'] as $video) {
echo $video['@attributes']['Description'];echo "<br>"; echo $video['Url'];echo "<br>";
}
And this all works great. The only time it doesn’t work is when there is only one Asset, and I can’t figure out how to compensate.
Doing the same loop to below does NOT work:
array(1) {
["Asset"]=>
array(3) {
["@attributes"]=>
array(26) {
["Title"]=>
string(13) "Jungle Cruise"
["Description"]=>
string(9) "Trailer 1"
["DateAdded"]=>
string(10) "2019-10-11"
["DateModified"]=>
string(10) "2019-10-11"
["TheatricalReleaseDate"]=>
string(10) "2020-07-24"
["Studio"]=>
string(35) "Walt Disney Studios Motion Pictures"
["Year"]=>
string(4) "2020"
["EClipId"]=>
string(7) "e165827"
["TrailerTypeId"]=>
string(1) "2"
["TrailerVersion"]=>
string(1) "1"
["LanguageName"]=>
string(7) "English"
["LanguageId"]=>
string(2) "52"
["MpaaRating"]=>
string(9) "Not Rated"
["MpaaRatingId"]=>
string(2) "34"
["Runtime"]=>
string(3) "140"
["RatingId"]=>
string(2) "30"
["ReleaseType"]=>
string(1) "2"
["BitrateId"]=>
string(3) "494"
["FormatId"]=>
string(2) "20"
["TitleId"]=>
string(6) "134947"
["ReleaseId"]=>
string(6) "175731"
["CountryId"]=>
string(3) "222"
["Country"]=>
string(13) "United States"
["Director"]=>
string(18) "Jaume Collet-Serra"
["GenreIds"]=>
string(15) "101,102,109,110"
["Genre"]=>
string(31) "Action,Adventure,Family,Fantasy"
}
["Identifiers"]=>
array(1) {
["@attributes"]=>
array(2) {
["Name"]=>
string(4) "IMDB"
["Value"]=>
string(9) "tt0870154"
}
}
["Url"]=>
string(81) "https://www.fileurl.com"
}
}
How do I get the loop to work on both var_dumps? No matter if 8 child elements or 1.
Cheers!
Ryan